client_Hale
2020-11-09 ce0512bf2fb02cc17f307294d6c77b21250e32d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.secondworld.universalsdk;
 
import com.secondworld.universalsdk.command.ICommand;
 
import org.json.JSONObject;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
 
import dalvik.system.DexFile;
 
 
public class H2EngineSDK {
 
    private static HashMap<Integer, ICommand> allCommand = new HashMap<Integer, ICommand>();
 
    /**
     * 初始化所有命令
     */
    public static void initCommandMap() {
        allCommand.clear();
        List<String> classesName = getClassName("com.secondworld.universalsdk.command");
        try {
            for (String name : classesName) {
                Class<?> aClass = Class.forName(name);
                if (!aClass.isInterface())
                    addCommand((ICommand) aClass.newInstance());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void addCommand(ICommand command) {
        allCommand.put(command.getCode(), command);
    }
 
    /**
     * unity 发来的消息
     *
     * @param json
     */
    public static void HandleUnityMessage(String json) {
        try {
            LogUtil.debug("HandleUnityMessage", json);
            JSONObject _json = new JSONObject(json);
            int code = _json.getInt("code");
            ICommand command = allCommand.get(code);
            if (command == null) {
 
                return;
            }
            command.process(json);
        } catch (Exception ignored) {
        }
    }
 
    /**
     * 通过反射读取指定包名下的所有类名
     *
     * @param packageName
     * @return
     */
    public static List<String> getClassName(String packageName) {
        List<String> classNameList = new ArrayList<String>();
        try {
            DexFile df = new DexFile(BaseApplication.APP.getPackageCodePath());//通过DexFile查找当前的APK中可执行文件
            Enumeration<String> enumeration = df.entries();//获取df中的元素  这里包含了所有可执行的类名 该类名包含了包名+类名的方式
            while (enumeration.hasMoreElements()) {//遍历
                String className = (String) enumeration.nextElement();
                if (className.contains(packageName)) {//在当前所有可执行的类里面查找包含有该包名的所有类
                    classNameList.add(className);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return classNameList;
    }
 
}