Example #1
0
        public ObjectCreator(Manifest manifest, InstalledMods mods)
        {
            typeCache = new Cache <string, Type>(FindType);
            ctorCache = new Cache <Type, ConstructorInfo>(GetCtor);

            var assemblyList = new List <Assembly>()
            {
                typeof(WarGame).Assembly
            };

//            foreach(var path in manifest.Assemblies)
//            {
//                var resolvedPath = FileSystem.FileSystem.ResolveAssemblyPath(path, manifest, mods);
//                if (resolvedPath == null)
//                    throw new FileNotFoundException("Assembly '{0}' not found.".F(path));

//                var data = Android.App.Application.Context.Assets.Open(resolvedPath);

//                MemoryStream memStream = new MemoryStream();
//                data.CopyTo(memStream);
//                memStream.Seek(0, SeekOrigin.Begin);
//                data.Close();
//                data = memStream;

//                var bytes = data.ReadAllBytes();

//                var hash = CryptoUtil.SHA1Hash(bytes);

//                Assembly assembly;
//                if(!ResolvedAssemblies.TryGetValue(hash,out assembly))
//                {
//#if DEBUG
//                    //var pdbPath = path.Replace(".dll", ".pdb");
//                    //var pdbData = modeFiles.Open(pdbPath).ReadAllBytes();

//                    //assembly = Assembly.Load(data, pdbData);
//                    //assembly = Assembly.ReflectionOnlyLoad(data);
//                    assembly = Assembly.Load(bytes);
//                   // var filepath = new FileInfo(Assembly.GetExecutingAssembly().Location);
//                   // Console.WriteLine("Assembly load name:" + filepath.FullName);
//                   // string filepath2 = "/storage/emulated/0/Android/data/com.eastwood.command/files/.__override__/EW.GameCenter.dll";
//                   // if (File.Exists(filepath2))
//                   // {
//                   //     var centerDll = Assembly.LoadFile(filepath2);
//                   // }

//                   // var strs = Android.App.Application.Context.GetExternalFilesDir("Content").Path;
//                   // strs = Path.Combine(strs, "test.txt");
//                   // var file = Android.App.Application.Context.GetExternalFilesDir("Content");
//                   // var fl = Android.App.Application.Context.FileList();
//                   // fl = Android.App.Application.Context.ApplicationContext.FileList();
//                   // if (file.Exists())
//                   // {
//                   //     Console.WriteLine(file.Path);
//                   // }
//                   // if (File.Exists(strs))
//                   // {
//                   //     Console.WriteLine("success");
//                   // }

//                   //assembly = Assembly.LoadFile(filepath2);
//                    //var path2 = "file:///android_asset/Content/mods/common/" + path.Split('|')[1];
//                    //var path2 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//                    //var fileName = System.IO.Path.Combine(path2, "Content/mods/common/"+path.Split('|')[1]);
//                    //var assembly2 = Assembly.LoadFrom(fileName);

//                    //assembly = Assembly.Load(data);

//                    //string path2 = Android.OS.Environment.ExternalStorageDirectory.Path;
//                    //string path2 = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
//                    //string path2 = Android.App.Application.Context.GetExternalFilesDir("").Path;
//                    //string filePath = System.IO.Path.Combine(path2, path.Split('|')[1]);
//                    //string filePath2 = System.IO.Path.Combine(path2, path.Split('|')[1].Replace(".dll", ".pdb"));
//                    //System.IO.File.WriteAllBytes(filePath, data);
//                    //System.IO.File.WriteAllBytes(filePath2, pdbData);
//                    //if (System.IO.File.Exists(filePath))
//                    //{
//                    //    Android.Util.Log.Debug("", "");
//                    //    assembly = Assembly.LoadFile(filePath);
//                    //}

//#else
//                    assembly = Assembly.Load(data);
//#endif
//                    ResolvedAssemblies.Add(hash, assembly);
//                }

//                assemblyList.Add(assembly);
//            }

            AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
            assemblies = assemblyList.SelectMany(asm => asm.GetNamespaces().Select(ns => Pair.New(asm, ns))).ToArray();
            AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly;
        }
Example #2
0
        public ModData(Manifest mod, InstalledMods mods, bool useLoadScreen = false)
        {
            Languages = new string[0];

            //local copy of the manifest
            Manifest       = new Manifest(mod.Id, mod.Package);
            ObjectCreator  = new ObjectCreator(Manifest, mods);
            PackageLoaders = ObjectCreator.GetLoaders <IPackageLoader>(Manifest.PackageFormats, "package");
            ModFiles       = new FileSystem.FileSystem(mods, PackageLoaders);
            ModFiles.LoadFromManifest(Manifest);
            Manifest.LoadCustomData(ObjectCreator);
            if (useLoadScreen)
            {
                LoadScreen = ObjectCreator.CreateObject <ILoadScreen>(Manifest.LoadScreen.Value);
                LoadScreen.Init(this, Manifest.LoadScreen.ToDictionary(my => my.Value));
                LoadScreen.Display();
            }

            WidgetLoader = new WidgetLoader(this);
            MapCache     = new MapCache(this);

            SoundLoaders  = ObjectCreator.GetLoaders <ISoundLoader>(Manifest.SoundFormats, "sound");
            SpriteLoaders = ObjectCreator.GetLoaders <ISpriteLoader>(Manifest.SpriteFormats, "sprite");

            var sequenceFormat = Manifest.Get <SpriteSequenceFormat>();
            var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader");
            var sequenceCtor   = sequenceLoader != null?sequenceLoader.GetConstructor(new[] { typeof(ModData) }) : null;

            if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || sequenceCtor == null)
            {
                throw new InvalidOperationException("Unable to find a sequence loader for type '{0}'.".F(sequenceFormat.Type));
            }
            SpriteSequenceLoader = (ISpriteSequenceLoader)sequenceCtor.Invoke(new[] { this });
            SpriteSequenceLoader.OnMissingSpriteError = s => { Console.WriteLine(s); };

            var modelFormat = Manifest.Get <ModelSequenceFormat>();
            var modelLoader = ObjectCreator.FindType(modelFormat.Type + "Loader");
            var modelCtor   = modelLoader != null?modelLoader.GetConstructor(new[] { typeof(ModData) }) : null;

            if (modelCtor == null || !modelLoader.GetInterfaces().Contains(typeof(IModelSequenceLoader)) || modelCtor == null)
            {
                throw new InvalidOperationException("Unable to find a model loader for type '{0}'".F(modelFormat.Type));
            }



            ModelSequenceLoader = (IModelSequenceLoader)modelCtor.Invoke(new[] { this });
            ModelSequenceLoader.OnMissingModelError = s => { };

            defaultRules = Exts.Lazy(() => Ruleset.LoadDefaults(this));

            //地形贴片集
            defaultTileSets = Exts.Lazy(() =>
            {
                var items = new Dictionary <string, TileSet>();
                foreach (var file in Manifest.TileSets)
                {
                    var t = new TileSet(DefaultFileSystem, file);
                    items.Add(t.Id, t);
                }
                return((IReadOnlyDictionary <string, TileSet>)(new ReadOnlyDictionary <string, TileSet>(items)));
            });

            //序列集
            defaultSequences = Exts.Lazy(() => {
                var items = DefaultTileSets.ToDictionary(t => t.Key, t => new SequenceProvider(DefaultFileSystem, this, t.Value, null));
                return((IReadOnlyDictionary <string, SequenceProvider>)(new ReadOnlyDictionary <string, SequenceProvider>(items)));
            });

            initialThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
        }