Esempio n. 1
0
        private void RealHandleMessage(byte[] messageData)
        {
            using (MessageReader mr = new MessageReader(messageData))
            {
                ModpackDataMessageType type = (ModpackDataMessageType)mr.Read <int>();
                switch (type)
                {
                case ModpackDataMessageType.CKAN:
                {
                    modpackMode = ModpackMode.CKAN;
                    byte[] receiveData = mr.Read <byte[]>();
                    byte[] oldData     = null;
                    if (File.Exists(ckanDataPath))
                    {
                        oldData = File.ReadAllBytes(ckanDataPath);
                    }
                    if (!BytesMatch(oldData, receiveData))
                    {
                        missingWarnFile = true;
                        DarkLog.Debug("Ckan file changed");
                        File.Delete(ckanDataPath);
                        File.WriteAllBytes(ckanDataPath, receiveData);
                    }
                    if (!registeredChatCommand)
                    {
                        registeredChatCommand = true;
                        chatWorker.RegisterChatCommand("upload", UploadCKANToServer, "Upload DarkMultiPlayer.ckan to the server");
                    }
                }
                break;

                case ModpackDataMessageType.MOD_LIST:
                {
                    modFilesToHash    = null;
                    modFilesToHashPos = 0;
                    serverPathCache.Clear();
                    noWarnSha.Clear();
                    modpackMode = ModpackMode.GAMEDATA;
                    string[] files = mr.Read <string[]>();
                    string[] sha   = mr.Read <string[]>();
                    if (File.Exists(gameDataServerCachePath))
                    {
                        File.Delete(gameDataServerCachePath);
                    }
                    using (StreamWriter sw = new StreamWriter(gameDataServerCachePath))
                    {
                        for (int i = 0; i < files.Length; i++)
                        {
                            bool skipFile = false;
                            foreach (string ignoreString in ignoreList)
                            {
                                if (files[i].ToLower().StartsWith(ignoreString, StringComparison.Ordinal))
                                {
                                    skipFile = true;
                                }
                            }
                            foreach (string ignoreString in containsIgnoreList)
                            {
                                if (files[i].ToLower().Contains(ignoreString))
                                {
                                    skipFile = true;
                                }
                            }
                            if (skipFile)
                            {
                                continue;
                            }
                            sw.WriteLine("{0}={1}", files[i], sha[i]);
                            serverPathCache.Add(files[i], sha[i]);
                        }
                    }
                    LoadAuto();
                    if (!registeredChatCommand)
                    {
                        registeredChatCommand = true;
                        chatWorker.RegisterChatCommand("upload", UploadToServer, "Upload GameData to the server");
                    }
                }
                break;

                case ModpackDataMessageType.REQUEST_OBJECT:
                {
                    modFilesToUpload    = mr.Read <string[]>();
                    modFilesToUploadPos = 0;
                    DarkLog.Debug("Server requested " + modFilesToUpload.Length + " files");
                }
                break;

                case ModpackDataMessageType.RESPONSE_OBJECT:
                {
                    string sha256sum = mr.Read <string>();
                    filesDownloaded++;
                    if (mr.Read <bool>())
                    {
                        syncString = "Syncing files " + filesDownloaded + "/" + requestList.Count + " (" + (serverPathCache.Count - requestList.Count) + " cached)";
                        byte[] fileBytes = mr.Read <byte[]>();
                        string filePath  = Path.Combine(cacheDataPath, sha256sum + ".bin");
                        if (!File.Exists(filePath))
                        {
                            File.WriteAllBytes(filePath, fileBytes);
                        }
                    }
                    else
                    {
                        ScreenMessages.PostScreenMessage("DMP Server has an out of date hash list. Tell the admin to run /reloadmods", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                        networkWorker.Disconnect("Syncing files error");
                    }
                    if (filesDownloaded == requestList.Count)
                    {
                        if (missingWarnFile)
                        {
                            networkWorker.Disconnect("Syncing files " + filesDownloaded + "/" + requestList.Count + " (" + (serverPathCache.Count - requestList.Count) + " cached)");
                            ScreenMessages.PostScreenMessage("Please run DMPModpackUpdater or reconnect to ignore", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                        }
                        else
                        {
                            synced = true;
                        }
                    }
                }
                break;

                case ModpackDataMessageType.MOD_DONE:
                {
                    if ((!missingWarnFile && requestList.Count == 0) || secondModSync)
                    {
                        synced = true;
                    }
                    else
                    {
                        if (modpackMode == ModpackMode.CKAN)
                        {
                            ScreenMessages.PostScreenMessage("Please install CKAN update at KSP/DarkMultiPlayer.ckan or reconnect to ignore", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                            networkWorker.Disconnect("Synced DarkMultiPlayer.ckan");
                        }
                        if (modpackMode == ModpackMode.GAMEDATA && requestList.Count == 0)
                        {
                            ScreenMessages.PostScreenMessage("Please run DMPModpackUpdater or reconnect to ignore", float.PositiveInfinity, ScreenMessageStyle.UPPER_CENTER);
                            syncString = "Synced files (" + serverPathCache.Count + " cached)";
                            networkWorker.Disconnect(syncString);
                        }
                    }
                    secondModSync = true;
                }
                break;
                }
            }
        }
Esempio n. 2
0
        public static void HandleModpackMessage(ClientObject client, byte[] messageData)
        {
            if (messageData == null || messageData.Length == 0)
            {
                ConnectionEnd.SendConnectionEnd(client, "Invalid mod control message from client");
                return;
            }

            if (!client.authenticated)
            {
                ConnectionEnd.SendConnectionEnd(client, "Unauthenticated client tried to send modpack message");
            }

            using (MessageReader mr = new MessageReader(messageData))
            {
                ModpackDataMessageType type = (ModpackDataMessageType)mr.Read <int>();
                switch (type)
                {
                case ModpackDataMessageType.CKAN:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    if (Settings.settingsStore.modpackMode != ModpackMode.CKAN)
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Please set server modpackMode to CKAN");
                        return;
                    }
                    byte[] fileBytes = mr.Read <byte[]>();
                    ModpackSystem.fetch.SaveCKANData(fileBytes);
                }
                break;

                case ModpackDataMessageType.MOD_LIST:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    if (Settings.settingsStore.modpackMode != ModpackMode.GAMEDATA)
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Please set server modpackMode to GAMEDATA");
                        return;
                    }
                    DarkLog.Normal("Modpack uploaded from " + client.playerName);
                    string[] files = mr.Read <string[]>();
                    string[] sha   = mr.Read <string[]>();
                    ModpackSystem.fetch.HandleNewGameData(files, sha, client);
                }
                break;

                case ModpackDataMessageType.REQUEST_OBJECT:
                {
                    string[] sha256sums = mr.Read <string[]>();
                    ModpackSystem.fetch.HandleSendList(client, sha256sums);
                }
                break;

                case ModpackDataMessageType.RESPONSE_OBJECT:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    string sha256sum = mr.Read <string>();
                    if (mr.Read <bool>())
                    {
                        byte[] fileBytes = mr.Read <byte[]>();
                        DarkLog.Debug("Received object: " + sha256sum);
                        ModpackSystem.fetch.SaveModObject(fileBytes, sha256sum);
                    }
                    else
                    {
                        DarkLog.Normal("Failed to recieve: " + sha256sum);
                    }
                }
                break;

                case ModpackDataMessageType.MOD_DONE:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    //Has gamedata upload
                    if (mr.Read <bool>())
                    {
                        DarkLog.Debug("Mod control file updated");
                        byte[] newModControl = mr.Read <byte[]>();
                        File.WriteAllBytes(Server.modFile, newModControl);
                    }
                    ModpackSystem.fetch.HandleModDone();
                }
                break;
                }
            }
        }