Ejemplo n.º 1
0
        internal static bool StartDownload(int id, string path, FileType type, int len, string md5hash, string resource)
        {
            if (CurrentFile != null)
            {
                LogManager.DebugLog("CurrentFile isn't null -- " + CurrentFile.Type + " " + CurrentFile.Filename);
                return(false);
            }

            if ((type == FileType.Normal || type == FileType.Script) && Directory.Exists(FileTransferId._DOWNLOADFOLDER_ + path.Replace(Path.GetFileName(path), "")) &&
                File.Exists(FileTransferId._DOWNLOADFOLDER_ + path))
            {
                byte[] myData;

                using (var md5 = MD5.Create())
                    using (var stream = File.OpenRead(FileTransferId._DOWNLOADFOLDER_ + path))
                    {
                        myData = md5.ComputeHash(stream);
                    }

                string hash = myData.Select(byt => byt.ToString("x2")).Aggregate((left, right) => left + right);

                FileIntegrity.Set(path, md5hash);

                if (hash == md5hash)
                {
                    if (type == FileType.Script)
                    {
                        PendingScripts.ClientsideScripts.Add(LoadScript(path, resource, File.ReadAllText(FileTransferId._DOWNLOADFOLDER_ + path)));
                    }

                    LogManager.DebugLog("HASH MATCHES, RETURNING FALSE");
                    return(false);
                }
            }

            CurrentFile = new FileTransferId(id, path, type, len, resource);
            return(true);
        }
Ejemplo n.º 2
0
        internal static void End(int id)
        {
            if (CurrentFile == null || CurrentFile.Id != id)
            {
                Util.Util.SafeNotify($"END Channel mismatch! We have {CurrentFile?.Id} and supplied was {id}");
                return;
            }

            try
            {
                if (CurrentFile.Type == FileType.Map)
                {
                    var obj = Main.DeserializeBinary <ServerMap>(CurrentFile.Data.ToArray()) as ServerMap;
                    if (obj == null)
                    {
                        Util.Util.SafeNotify("ERROR DOWNLOADING MAP: NULL");
                    }
                    else
                    {
                        Main.AddMap(obj);
                    }
                }
                else if (CurrentFile.Type == FileType.Script)
                {
                    try
                    {
                        var scriptText = Encoding.UTF8.GetString(CurrentFile.Data.ToArray());
                        var newScript  = LoadScript(CurrentFile.Filename, CurrentFile.Resource, scriptText);
                        PendingScripts.ClientsideScripts.Add(newScript);
                    }
                    catch (ArgumentException)
                    {
                        CurrentFile.Dispose();
                        if (File.Exists(CurrentFile.FilePath))
                        {
                            try { File.Delete(CurrentFile.FilePath); }
                            catch { }
                        }
                    }
                }
                else if (CurrentFile.Type == FileType.EndOfTransfer)
                {
                    try
                    {
                        if (Main.JustJoinedServer)
                        {
                            World.RenderingCamera           = null;
                            Main.MainMenu.TemporarilyHidden = false;
                            Main.MainMenu.Visible           = false;
                            Main.JustJoinedServer           = false;
                        }

                        List <string> AffectedResources = new List <string>();
                        AffectedResources.AddRange(PendingScripts.ClientsideScripts.Select(cs => cs.ResourceParent));

                        Main.StartClientsideScripts(PendingScripts);
                        PendingScripts.ClientsideScripts.Clear();

                        Main.InvokeFinishedDownload(AffectedResources);
                    }
                    catch (Exception e)
                    {
                        LogManager.LogException(e, "DownloadManager");
                    }
                }
                else if (CurrentFile.Type == FileType.CustomData)
                {
                    string data = Encoding.UTF8.GetString(CurrentFile.Data.ToArray());

                    JavascriptHook.InvokeCustomDataReceived(CurrentFile.Resource, data);
                }
            }
            finally
            {
                CurrentFile.Dispose();

                if (CurrentFile.Type == FileType.Normal && File.Exists(CurrentFile.FilePath))
                {
                    var mime = MimeTypes.GetMimeType(File.ReadAllBytes(CurrentFile.FilePath), CurrentFile.FilePath);

                    if (!_allowedFiletypes.Contains(mime))
                    {
                        try { File.Delete(CurrentFile.FilePath); }
                        catch { }

                        Screen.ShowNotification("Disallowed file type: " + mime + "~n~" + CurrentFile.Filename);
                    }
                }

                CurrentFile = null;
            }
        }
Ejemplo n.º 3
0
 internal static void Cancel()
 {
     CurrentFile = null;
 }