public override void OnStartup()
        {
            string dataPath = FolderPath;

            _dataPath = dataPath;

            GetOrCreateZippedModsFolder();
            GetOrCreateModsFolder();
            GetOrCreateSpecialModsDataFolder();

            string modsFolder = GetOrCreateModsFolder();

            string[] modFolders = Directory.GetDirectories(modsFolder);
            foreach (string folder in modFolders)
            {
                string dataFilePath = folder + "/" + ModsManager.MOD_INFO_FILE_NAME;
                if (!File.Exists(dataFilePath))
                {
                    continue;
                }

                ModInfo loadedModInfo;
                string  json = File.ReadAllText(dataFilePath);
                try
                {
                    loadedModInfo = JsonConvert.DeserializeObject <ModInfo>(json);
                }
                catch (JsonException e)
                {
                    OutputConsole.WriteLine("Failed to read mod data file for mod: " + folder);
                    continue;
                }
                if (!loadedModInfo.AreAllEssentialFieldsAssigned(out string msg))
                {
                    OutputConsole.WriteLine("Missing fields for mod: " + folder + ", " + msg);
                    continue;
                }
                loadedModInfo.FixFieldValues();

                _loadedMods.Add(loadedModInfo.UniqueID, loadedModInfo);
                _loadedModJsons.Add(loadedModInfo.UniqueID, json);
            }

            string specialModDataFolder = GetOrCreateSpecialModsDataFolder();

            string[] specialModDataFiles = Directory.GetFiles(specialModDataFolder);
            foreach (string specialModDatFile in specialModDataFiles)
            {
                string         json           = File.ReadAllText(specialModDatFile);
                SpecialModData specialModData = SpecialModData.FromJson(json);
                _loadedSpecialModData.Add(specialModData.ModId, specialModData);
            }

            foreach (KeyValuePair <string, ModInfo> loadedMod in _loadedMods)
            {
                ZipMod(loadedMod.Key);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            ShutdownHandlerOverrider.Init();

            PopulateOperations();
            PopulateOwnFolderObjects();

            Directory.CreateDirectory(BasePath);

            Directory.CreateDirectory(ModTemplateFilePath);
            Directory.CreateDirectory(WebsitePath);

            if (args.Contains("-httpOnly"))
            {
                HttpListener httpMainListener = new HttpListener();
                httpMainListener.Prefixes.Add("http://+:80/");
                httpMainListener.Start();
                listenMain(httpMainListener);

                OutputConsole.WriteLine("Listening...");
                OutputConsole.WriteLine("WARNING: You are currently running in http only mode, this is only intended for test use, do not do this in production.");

                while (ShutdownHandlerOverrider.ShouldKeepRunning)
                {
                    Thread.Sleep(10);
                }
            }


            HttpListener httpsListener = new HttpListener();

            //httpListener.Prefixes.Add("http://+:80/");
            httpsListener.Prefixes.Add("https://+:443/");
            httpsListener.Start();
            listenMain(httpsListener);

            OutputConsole.WriteLine("Listening...");

            HttpListener httpListener = new HttpListener();

            httpListener.Prefixes.Add("http://+:80/");
            httpListener.Start();
            listenHttp(httpListener);

            while (ShutdownHandlerOverrider.ShouldKeepRunning)
            {
                Thread.Sleep(10);
            }
        }
        public bool TryUploadModFromZip(byte[] zipData, string sessionID, out ModInfo modInfo, out string error)
        {
            ModInfo loadedModInfo;

            try
            {
                string tempPath = Path.GetTempPath();
                string guid     = Guid.NewGuid().ToString();

                string tempZipPath = tempPath + guid + ".zip";
                string tempModPath = tempPath + guid + "/";
                Directory.CreateDirectory(tempModPath);

                File.WriteAllBytes(tempZipPath, zipData);
                ZipFile.ExtractToDirectory(tempZipPath, tempModPath);


                string[] directories = Directory.GetDirectories(tempModPath);
                if (Directory.GetFiles(tempModPath).Length == 0 && directories.Length == 1) // if there is a single folder and no files then the mod is probably in that folder
                {
                    tempModPath = directories[0];
                }

                string modInfoPath = tempModPath + ModsManager.MOD_INFO_FILE_NAME;

                if (!File.Exists(modInfoPath))
                {
                    error   = "Could not find the " + ModsManager.MOD_INFO_FILE_NAME + " file :/";
                    modInfo = null;
                    return(false);
                }

                string json = File.ReadAllText(modInfoPath);
                try
                {
                    loadedModInfo = JsonConvert.DeserializeObject <ModInfo>(json);
                }
                catch (JsonException e)
                {
                    error   = "Error deserializing" + ModsManager.MOD_INFO_FILE_NAME + " file";
                    modInfo = null;
                    return(false);
                }
                if (!loadedModInfo.AreAllEssentialFieldsAssigned(out string msg))
                {
                    error   = msg;
                    modInfo = null;
                    return(false);
                }
                loadedModInfo.FixFieldValues();

                string playerId = SessionsManager.Instance.GetPlayerIdFromSession(sessionID);

                SpecialModData oldModWithSameID = GetSpecialModInfoFromId(loadedModInfo.UniqueID);
                if (oldModWithSameID != null)
                {
                    if (oldModWithSameID.OwnerID != playerId)
                    {
                        error   = "You cannot override a mod that you do not own";
                        modInfo = null;
                        return(false);
                    }
                }

                string newFolderPath = GetModFolderPath(loadedModInfo.UniqueID);
                if (Directory.Exists(newFolderPath))
                {
                    Utils.RecursivelyDeleteFolder(newFolderPath);
                }

                Directory.CreateDirectory(newFolderPath);

                Utils.CopyFilesRecursively(new DirectoryInfo(tempModPath), new DirectoryInfo(newFolderPath));

                File.Delete(tempZipPath);
                Utils.RecursivelyDeleteFolder(tempModPath);
            }
            catch (Exception e)
            {
                modInfo = null;
                error   = "something went wrong while handaling the zip file";
                OutputConsole.WriteLine(e.ToString());
                return(false);
            }

            if (_loadedMods.ContainsKey(loadedModInfo.UniqueID))
            {
                _loadedMods.Remove(loadedModInfo.UniqueID);
            }
            if (_loadedModJsons.ContainsKey(loadedModInfo.UniqueID))
            {
                _loadedModJsons.Remove(loadedModInfo.UniqueID);
            }

            _loadedMods.Add(loadedModInfo.UniqueID, loadedModInfo);
            _loadedModJsons.Add(loadedModInfo.UniqueID, JsonConvert.SerializeObject(loadedModInfo));

            ZipMod(loadedModInfo.UniqueID);

            if (_loadedSpecialModData.ContainsKey(loadedModInfo.UniqueID))
            {
                _loadedSpecialModData[loadedModInfo.UniqueID].UpdatedDate = (ulong)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            }
            else
            {
                CreateAndAddSpecialModDataFormMod(loadedModInfo.UniqueID, SessionsManager.Instance.GetPlayerIdFromSession(sessionID));
            }

            modInfo = loadedModInfo;
            error   = null;
            return(true);
        }