public UnitStartInfo(UnitInfo unitDef, Player player, double x, double y): base(x, y)
		{
			this.unitDef = unitDef;
			this.player = player;
			unitDefName = unitDef.Name;
			groups = new ObservableCollection<string>();
		}
        UnitInfo[] ReadUnits()
        {
            var unitInfos = new List<UnitInfo>();

            if (disposed) throw new ObjectDisposedException("Unitsync has already been released.");
            if (!NativeMethods.lpOpenFile("gamedata/defs.lua", VfsMode.Zip, VfsMode.Zip)) throw new UnitSyncException("Error parsing defs.lua: " + NativeMethods.lpErrorLog());
            SetLoadingStatus("Loading Mod (Unit Definitions)");
            if (!NativeMethods.lpExecute()) throw new UnitSyncException("Unable to read defs.lua: " + NativeMethods.lpErrorLog());
            if (!NativeMethods.lpSubTableStr("unitdefs")) throw new UnitSyncException(); // push unitdefs

            for (var unitKeyIndex = 0; unitKeyIndex < NativeMethods.lpGetStrKeyListCount(); unitKeyIndex++)
            {
                var unitName = NativeMethods.lpGetStrKeyListEntry(unitKeyIndex);
                if (!NativeMethods.lpSubTableStr(unitName)) throw new UnitSyncException(); // push unitdef
                var unitInfo = new UnitInfo();
                unitInfo.Name = unitName;
                if (NativeMethods.lpGetKeyExistsStr("name")) unitInfo.FullName = NativeMethods.lpGetStrKeyStrVal("name", String.Empty);
                if (NativeMethods.lpGetKeyExistsStr("buildpic")) unitInfo.BuildPicField = NativeMethods.lpGetStrKeyStrVal("buildpic", String.Empty);
                unitInfo.FootprintX = NativeMethods.lpGetStrKeyIntVal("footprintx", 1);
                unitInfo.FootprintY = NativeMethods.lpGetStrKeyIntVal("footprintz", 1);

                if (NativeMethods.lpGetKeyExistsStr("customparams"))
                {
                    if (!NativeMethods.lpSubTableStr("customparams")) throw new UnitSyncException(); // push customparams
                    if (NativeMethods.lpGetKeyExistsStr("hideinmissioneditor"))
                    {
                        var type = (LuaType)NativeMethods.lpGetStrKeyType("hideinmissioneditor");
                        if (type == LuaType.Boolean) unitInfo.Hide = NativeMethods.lpGetStrKeyBoolVal("hideinmissioneditor", 0);
                        else if (type == LuaType.String) unitInfo.Hide = NativeMethods.lpGetStrKeyStrVal("hideinmissioneditor", String.Empty) != "false";
                        else unitInfo.Hide = true;
                    }
                    NativeMethods.lpPopTable(); // pop customparams
                }

                if (NativeMethods.lpGetKeyExistsStr("buildoptions"))
                {
                    if (NativeMethods.lpGetKeyExistsStr("yardmap")) unitInfo.IsFactory = true;
                    if (!NativeMethods.lpSubTableStr("buildoptions")) throw new UnitSyncException(); // push buildoptions
                    var unitList = new List<string>();
                    for (var buildOptionIndex = 0; buildOptionIndex < NativeMethods.lpGetIntKeyListCount(); buildOptionIndex++)
                    {
                        var key = NativeMethods.lpGetIntKeyListEntry(buildOptionIndex);
                        var buildOption = NativeMethods.lpGetIntKeyStrVal(key, String.Empty);
                        unitList.Add(buildOption);
                    }
                    NativeMethods.lpPopTable(); // pop buildoptions
                    unitInfo.BuildOptions = unitList;
                }
                // lazy hide: pretend the unit does not exist
                if (!unitInfo.Hide) unitInfos.Add(unitInfo);
                NativeMethods.lpPopTable(); // pop unitdef
            }
            NativeMethods.lpPopTable(); // pop unitdefs
            NativeMethods.lpClose();
            foreach (var unitInfo in unitInfos) GetBuildPic(unitInfo);
            return unitInfos.ToArray();
        }
        IEnumerable<UnitInfo> GetUnitList(int modIndex)
        {
            SetLoadingStatus("Loading Mod (Unit List)");
            if (modIndex != loadedArchiveIndex)
            {
                NativeMethods.AddAllArchives(NativeMethods.GetPrimaryModArchive(modIndex));
                loadedArchiveIndex = modIndex;
            }
            for (var i = 0; i <= MaxUnits && NativeMethods.ProcessUnitsNoChecksum() > 0; i++)
            {
                var error = NativeMethods.GetNextError();
                if (error != null)
                {
                    Debug.WriteLine("UnitSync Error: " + error);
                    break;
                }
            }
            var units = new List<UnitInfo>();
            for (var i = 0; i < NativeMethods.GetUnitCount(); i++)
            {
                var unitName = NativeMethods.GetUnitName(i);
                var fullUnitName = NativeMethods.GetFullUnitName(i);
                var unitInfo = new UnitInfo { Name = unitName, FullName = fullUnitName };

                units.Add(unitInfo);
            }

            foreach (var unit in units) GetBuildPic(unit);
            return units;
        }
        void GetBuildPic(UnitInfo unit)
        {
            if (!String.IsNullOrEmpty(unit.BuildPicField))
            {
                var data = ReadVfsFile("unitpics/" + unit.BuildPicField);
                if (data != null)
                {
                    try
                    {
                        unit.BuildPic = LoadBuildpic(data);
                        return;
                    }
                    catch {}
                }
            }

            // buildpic not specified, try guessing the buildpic
            var files = GetFilesInVfsDirectory("unitpics", unit.Name + "*", VfsMode.Zip);
            if (files.Any())
            {
                foreach (var fileName in files)
                {
                    var fileData = ReadVfsFile(fileName);
                    if (fileData != null)
                    {
                        try
                        {
                            unit.BuildPic = LoadBuildpic(fileData);
                            return;
                        }
                        catch {}
                    }
                }
            }
        }
Exemple #5
0
        UnitInfo[] ReadUnits()
        {
            var unitInfos = new List <UnitInfo>();

            if (disposed)
            {
                throw new ObjectDisposedException("Unitsync has already been released.");
            }
            if (!NativeMethods.lpOpenFile("gamedata/defs.lua", VfsMode.Mod, VfsMode.Mod))
            {
                throw new UnitSyncException("Error parsing defs.lua: " + NativeMethods.lpErrorLog());
            }
            SetLoadingStatus("Loading Mod (Unit Definitions)");
            if (!NativeMethods.lpExecute())
            {
                throw new UnitSyncException("Unable to read  defs.lua: " + NativeMethods.lpErrorLog());
            }
            if (!NativeMethods.lpSubTableStr("unitdefs"))
            {
                throw new UnitSyncException();                                           // push unitdefs
            }
            for (var unitKeyIndex = 0; unitKeyIndex < NativeMethods.lpGetStrKeyListCount(); unitKeyIndex++)
            {
                var unitName = NativeMethods.lpGetStrKeyListEntry(unitKeyIndex);
                if (!NativeMethods.lpSubTableStr(unitName))
                {
                    throw new UnitSyncException();                                         // push unitdef
                }
                var unitInfo = new UnitInfo();
                unitInfo.Name = unitName;
                if (NativeMethods.lpGetKeyExistsStr("name"))
                {
                    unitInfo.FullName = NativeMethods.lpGetStrKeyStrVal("name", String.Empty);
                }
                if (NativeMethods.lpGetKeyExistsStr("buildpic"))
                {
                    unitInfo.BuildPicField = NativeMethods.lpGetStrKeyStrVal("buildpic", String.Empty);
                }
                unitInfo.FootprintX = NativeMethods.lpGetStrKeyIntVal("footprintx", 1);
                unitInfo.FootprintY = NativeMethods.lpGetStrKeyIntVal("footprintz", 1);

                if (NativeMethods.lpGetKeyExistsStr("customparams"))
                {
                    if (!NativeMethods.lpSubTableStr("customparams"))
                    {
                        throw new UnitSyncException();                                               // push customparams
                    }
                    if (NativeMethods.lpGetKeyExistsStr("hideinmissioneditor"))
                    {
                        var type = (LuaType)NativeMethods.lpGetStrKeyType("hideinmissioneditor");
                        if (type == LuaType.Boolean)
                        {
                            unitInfo.Hide = NativeMethods.lpGetStrKeyBoolVal("hideinmissioneditor", 0);
                        }
                        else if (type == LuaType.String)
                        {
                            unitInfo.Hide = NativeMethods.lpGetStrKeyStrVal("hideinmissioneditor", String.Empty) != "false";
                        }
                        else
                        {
                            unitInfo.Hide = true;
                        }
                    }
                    NativeMethods.lpPopTable(); // pop customparams
                }

                if (NativeMethods.lpGetKeyExistsStr("buildoptions"))
                {
                    if (NativeMethods.lpGetKeyExistsStr("yardmap"))
                    {
                        unitInfo.IsFactory = true;
                    }
                    if (!NativeMethods.lpSubTableStr("buildoptions"))
                    {
                        throw new UnitSyncException();                                               // push buildoptions
                    }
                    var unitList = new List <string>();
                    for (var buildOptionIndex = 0; buildOptionIndex < NativeMethods.lpGetIntKeyListCount(); buildOptionIndex++)
                    {
                        var key         = NativeMethods.lpGetIntKeyListEntry(buildOptionIndex);
                        var buildOption = NativeMethods.lpGetIntKeyStrVal(key, String.Empty);
                        unitList.Add(buildOption);
                    }
                    NativeMethods.lpPopTable(); // pop buildoptions
                    unitInfo.BuildOptions = unitList;
                }
                // lazy hide: pretend the unit does not exist
                if (!unitInfo.Hide)
                {
                    unitInfos.Add(unitInfo);
                }
                NativeMethods.lpPopTable(); // pop unitdef
            }
            NativeMethods.lpPopTable();     // pop unitdefs
            NativeMethods.lpClose();
            foreach (var unitInfo in unitInfos)
            {
                GetBuildPic(unitInfo);
            }
            return(unitInfos.ToArray());
        }