Commit [50461e] Maximize Restore History

Load mods from disk and compile them on start.

exe exe 2012-07-31

added ScriptingApi/Properties/AssemblyInfo.cs
added ScriptingApi/ModManager.cs
added ScriptingApi/ScriptingApi.csproj
added ScriptingApi/a.snk
changed ManicDiggerLib/Mods/BuildLog.cs
changed ManicDiggerLib/Mods/DefaultWorldGenerator.cs
changed ManicDiggerLib/Mods/Revert.cs
changed ManicDiggerLib/Mods/Tnt.cs
changed ManicDiggerLib/Mods/VandalFinder.cs
changed ManicDiggerLib/ManicDiggerLib.csproj
changed ManicDiggerLib/Misc.cs
changed ManicDiggerLib/Mod.cs
changed ManicDiggerLib/Server.cs
changed ManicDigger.sln
changed build.bat
copied ManicDiggerLib/Mods/FlatWorldGenerator.cs -> ManicDiggerLib/ModLoader.cs
ScriptingApi/Properties/AssemblyInfo.cs Diff Switch to side-by-side view
Loading...
ScriptingApi/ModManager.cs Diff Switch to side-by-side view
Loading...
ScriptingApi/ScriptingApi.csproj Diff Switch to side-by-side view
Loading...
ScriptingApi/a.snk Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Mods/BuildLog.cs Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Mods/DefaultWorldGenerator.cs Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Mods/Revert.cs Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Mods/Tnt.cs Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Mods/VandalFinder.cs Diff Switch to side-by-side view
Loading...
ManicDiggerLib/ManicDiggerLib.csproj Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Misc.cs Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Mod.cs Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Server.cs Diff Switch to side-by-side view
Loading...
ManicDigger.sln Diff Switch to side-by-side view
Loading...
build.bat Diff Switch to side-by-side view
Loading...
ManicDiggerLib/Mods/FlatWorldGenerator.cs to ManicDiggerLib/ModLoader.cs
--- a/ManicDiggerLib/Mods/FlatWorldGenerator.cs
+++ b/ManicDiggerLib/ModLoader.cs
@@ -1,65 +1,100 @@
 ďťżusing System;
 using System.Collections.Generic;
 using System.Text;
+using Microsoft.CSharp;
+using System.CodeDom.Compiler;
 
-namespace ManicDigger.Mods
+namespace ManicDigger
 {
-    public class FlatWorldGenerator : IMod
+    public class ModLoader
     {
-        public void PreStart(ModManager m)
+        public void CompileScripts(Dictionary<string, string> scripts)
         {
-            m.RequireMod("Default");
-        }
-        public void Start(ModManager m)
-        {
-            this.m = m;
-            m.RegisterWorldGenerator(GetChunk);
-        }
-        ModManager m;
-        void GetChunk(int x, int y, int z, byte[] chunk)
-        {
-            x *= m.GetChunkSize();
-            y *= m.GetChunkSize();
-            z *= m.GetChunkSize();
-            int mapsizez = m.GetMapSizeZ();
-            int chunksize = m.GetChunkSize();
-            int adminium = m.GetBlockId("Adminium");
-            int stone = m.GetBlockId("Stone");
-            int dirt = m.GetBlockId("Dirt");
-            int grass = m.GetBlockId("Grass");
-            for (int xx = 0; xx < chunksize; xx++)
+            foreach (var k in scripts)
             {
-                for (int yy = 0; yy < chunksize; yy++)
+                CSharpCodeProvider compiler = new CSharpCodeProvider(new Dictionary<String, String>{{ "CompilerVersion", "v3.5" }});
+                var parms = new CompilerParameters
                 {
-                    for (int zz = 0; zz < chunksize; zz++)
+                    GenerateExecutable = false,
+                    GenerateInMemory = true,
+                };
+                parms.ReferencedAssemblies.Add("System.dll");
+                parms.ReferencedAssemblies.Add("ScriptingApi.dll");
+                parms.ReferencedAssemblies.Add("LibNoise.dll");
+                CompilerResults results = compiler.CompileAssemblyFromSource(parms, new string[] { k.Value });
+                if (results.Errors.Count != 0)
+                {
+                    try
                     {
-                        int block;
-                        int globalz = z + zz;
-                        if (globalz == 0)
+                        string errors = "";
+                        foreach (CompilerError error in results.Errors)
                         {
-                            block = adminium;
+                            if (error.IsWarning)
+                            {
+                                continue;
+                            }
+                            errors += string.Format("{0} Line:{1} {2}",error.ErrorNumber, error.Line, error.ErrorText);
                         }
-                        else if (globalz < mapsizez / 2 - 5)
-                        {
-                            block = stone;
-                        }
-                        else if (globalz < mapsizez / 2)
-                        {
-                            block = dirt;
-                        }
-                        else if (globalz == mapsizez / 2)
-                        {
-                            block = grass;
-                        }
-                        else
-                        {
-                            block = 0;
-                        }
-                        int pos = ModManager.Index3d(xx, yy, zz, chunksize, chunksize);
-                        chunk[pos] = (byte)block;
+                        System.Windows.Forms.MessageBox.Show("Can't load mod: " + k.Key + "\n"+errors);
+                    }
+                    catch
+                    {
+                    }
+                    continue;
+                }
+                foreach (Type t in results.CompiledAssembly.GetTypes())
+                {
+                    if (typeof(IMod).IsAssignableFrom(t))
+                    {
+                        mods[t.Name] = (IMod)results.CompiledAssembly.CreateInstance(t.FullName);
                     }
                 }
             }
         }
+
+        Dictionary<string, IMod> mods = new Dictionary<string, IMod>();
+        Dictionary<string, string[]> modRequirements = new Dictionary<string, string[]>();
+        Dictionary<string, bool> loaded = new Dictionary<string, bool>();
+
+        public void Start(ModManager m, List<string> currentRequires)
+        {
+            /*
+            foreach (var mod in mods)
+            {
+                mod.Start(m);
+            }
+            */
+            foreach (var k in mods)
+            {
+                k.Value.PreStart(m);
+                modRequirements[k.Key] = currentRequires.ToArray();
+                currentRequires.Clear();
+            }
+            foreach (var k in mods)
+            {
+                StartMod(k.Key, k.Value, m);
+            }
+        }
+
+        void StartMod(string name, IMod mod, ModManager m)
+        {
+            if (loaded.ContainsKey(name))
+            {
+                return;
+            }
+            if (modRequirements.ContainsKey(name))
+            {
+                foreach (string required_name in modRequirements[name])
+                {
+                    if (!mods.ContainsKey(required_name))
+                    {
+                        System.Windows.Forms.MessageBox.Show(string.Format("Can't load mod {0} because its dependency {1} couldn't be loaded.", name, required_name));
+                    }
+                    StartMod(required_name, mods[required_name], m);
+                }
+            }
+            mod.Start(m);
+            loaded[name] = true;
+        }
     }
 }