示例#1
0
        static bool Prefix(FSMCoreGame __instance, ScriptBundleLoader manager, IEnumerable <string> filePaths)
        {
            UnityEngine.Debug.Log("ScriptBundleLoader.ScriptBundle Patched true");

            AccessTools.Field(typeof(ScriptBundleLoader.ScriptBundle), "fileSystemWatchers").SetValue(__instance, new List <FileSystemWatcher>());
            AccessTools.Field(typeof(ScriptBundleLoader.ScriptBundle), "instances").SetValue(__instance, new List <object>());

            List <string> fullPaths = new List <string>();

            foreach (string value in filePaths)
            {
                fullPaths.Add(Path.GetFullPath(value));
            }

            AccessTools.Field(typeof(ScriptBundleLoader.ScriptBundle), "filePaths").SetValue(__instance, fullPaths);
            AccessTools.Field(typeof(ScriptBundleLoader.ScriptBundle), "manager").SetValue(__instance, manager);

            AppDomain currentDomain = AppDomain.CurrentDomain;

            Assembly[] assemblies = currentDomain.GetAssemblies();

            List <string> list = new List <string>();

            for (int i = 0; i < assemblies.Count(); i++)
            {
                string result;
                try
                {
                    result = assemblies[i].Location;
                }
                catch
                {
                    result = null;
                }
                if (result != null)
                {
                    list.Add(result);
                }
            }

            Debug.Log("domains list size " + list.Count);

            AccessTools.Field(typeof(ScriptBundleLoader.ScriptBundle), "assemblyReferences").SetValue(__instance, list.ToArray());

            if (HoneyDebug.Get().loadingPerformance)
            {
                manager.logWriter.WriteLine("loading " + Environment.NewLine + string.Join(Environment.NewLine, filePaths.ToArray <string>()));
            }

            AccessTools.Method(typeof(ScriptBundleLoader.ScriptBundle), "CompileFiles").Invoke(__instance, null);
            AccessTools.Method(typeof(ScriptBundleLoader.ScriptBundle), "CreateFileWatchers").Invoke(__instance, null);
            AccessTools.Method(typeof(ScriptBundleLoader.ScriptBundle), "CreateNewInstances").Invoke(__instance, null);

            return(false);
        }
示例#2
0
    public void Compile(GameObject textArea, string scriptName, GameObject attachTarget)
    {
        File.WriteAllText(Application.streamingAssetsPath + "/" + scriptName, input.text);
        consoleOutput.text = "";

        synchronizedInvoke = new DeferredSynchronizeInvoke();

        loader = new ScriptBundleLoader(synchronizedInvoke)
        {
            logWriter      = new UnityLogTextWriter(),
            createInstance = (Type t) => {
                if (typeof(Component).IsAssignableFrom(t))
                {
                    string scriptToDelete = "";

                    switch (attachTarget.gameObject.tag)
                    {
                    case "Player":
                        scriptToDelete = "GunAddOn";
                        break;

                    case "Bullet":
                        scriptToDelete = "BulletAddOn";
                        break;
                    }

                    foreach (Component component in attachTarget.GetComponents(typeof(Component)))
                    {
                        if (component.GetType().ToString() == scriptToDelete)
                        {
                            Destroy(attachTarget.GetComponent(component.GetType()));
                        }
                    }

                    return(attachTarget.AddComponent(t));
                }
                else
                {
                    return(Activator.CreateInstance(t));
                }
            },
            destroyInstance = (object instance) => {
                if (instance is Component)
                {
                    Destroy(instance as Component);
                }
            }
        };

        try {
            var sourceFolder = Application.streamingAssetsPath;
            var files        = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                if (file.EndsWith(scriptName))
                {
                    loader.LoadAndWatchScriptsBundle(new[] { file });
                }
            }
            consoleOutput.text = "Compile successful";
        }
        catch (Exception e) {
            consoleOutput.text = e.ToString();
        }

        synchronizedInvoke = new DeferredSynchronizeInvoke();
    }