예제 #1
0
        public static void PackWorld(string directoryName, Stream targetStream, Func <string, bool> filter, bool embedExternalContent)
        {
            WorldInfo worldInfo = GetWorldInfo(directoryName);

            if (worldInfo == null)
            {
                throw new InvalidOperationException("Directory does not contain a world.");
            }
            List <string> list = new List <string>();

            RecursiveEnumerateDirectory(directoryName, list, null, filter);
            using (ZipArchive zipArchive = ZipArchive.Create(targetStream, keepStreamOpen: true))
            {
                foreach (string item in list)
                {
                    using (Stream source = Storage.OpenFile(item, OpenFileMode.Read))
                    {
                        string fileName = Storage.GetFileName(item);
                        zipArchive.AddStream(fileName, source);
                    }
                }
                if (embedExternalContent)
                {
                    if (!BlocksTexturesManager.IsBuiltIn(worldInfo.WorldSettings.BlocksTextureName))
                    {
                        try
                        {
                            using (Stream source2 = Storage.OpenFile(BlocksTexturesManager.GetFileName(worldInfo.WorldSettings.BlocksTextureName), OpenFileMode.Read))
                            {
                                string filenameInZip = Storage.CombinePaths("EmbeddedContent", Storage.GetFileNameWithoutExtension(worldInfo.WorldSettings.BlocksTextureName) + ".scbtex");
                                zipArchive.AddStream(filenameInZip, source2);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Warning($"Failed to embed blocks texture \"{worldInfo.WorldSettings.BlocksTextureName}\". Reason: {ex.Message}");
                        }
                    }
                    foreach (PlayerInfo playerInfo in worldInfo.PlayerInfos)
                    {
                        if (!CharacterSkinsManager.IsBuiltIn(playerInfo.CharacterSkinName))
                        {
                            try
                            {
                                using (Stream source3 = Storage.OpenFile(CharacterSkinsManager.GetFileName(playerInfo.CharacterSkinName), OpenFileMode.Read))
                                {
                                    string filenameInZip2 = Storage.CombinePaths("EmbeddedContent", Storage.GetFileNameWithoutExtension(playerInfo.CharacterSkinName) + ".scskin");
                                    zipArchive.AddStream(filenameInZip2, source3);
                                }
                            }
                            catch (Exception ex2)
                            {
                                Log.Warning($"Failed to embed character skin \"{playerInfo.CharacterSkinName}\". Reason: {ex2.Message}");
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
 public static void ShowUploadUi(ExternalContentType type, string name)
 {
     DialogsManager.ShowDialog(null, new SelectExternalContentProviderDialog(LanguageControl.Get(fName, 9), listingSupportRequired : false, delegate(IExternalContentProvider provider)
     {
         try
         {
             if (provider != null)
             {
                 ShowLoginUiIfNeeded(provider, showWarningDialog: true, delegate
                 {
                     CancellableBusyDialog busyDialog = new CancellableBusyDialog(LanguageControl.Get(fName, 10), autoHideOnCancel: false);
                     DialogsManager.ShowDialog(null, busyDialog);
                     Task.Run(delegate
                     {
                         bool needsDelete  = false;
                         string sourcePath = null;
                         Stream stream     = null;
                         Action cleanup    = delegate
                         {
                             Utilities.Dispose(ref stream);
                             if (needsDelete && sourcePath != null)
                             {
                                 try
                                 {
                                     Storage.DeleteFile(sourcePath);
                                 }
                                 catch
                                 {
                                 }
                             }
                         };
                         try
                         {
                             string path;
                             if (type == ExternalContentType.BlocksTexture)
                             {
                                 sourcePath = BlocksTexturesManager.GetFileName(name);
                                 if (sourcePath == null)
                                 {
                                     throw new InvalidOperationException(LanguageControl.Get(fName, 11));
                                 }
                                 path = Storage.GetFileName(sourcePath);
                             }
                             else if (type == ExternalContentType.CharacterSkin)
                             {
                                 sourcePath = CharacterSkinsManager.GetFileName(name);
                                 if (sourcePath == null)
                                 {
                                     throw new InvalidOperationException(LanguageControl.Get(fName, 11));
                                 }
                                 path = Storage.GetFileName(sourcePath);
                             }
                             else if (type == ExternalContentType.FurniturePack)
                             {
                                 sourcePath = FurniturePacksManager.GetFileName(name);
                                 if (sourcePath == null)
                                 {
                                     throw new InvalidOperationException(LanguageControl.Get(fName, 11));
                                 }
                                 path = Storage.GetFileName(sourcePath);
                             }
                             else
                             {
                                 if (type != ExternalContentType.World)
                                 {
                                     throw new InvalidOperationException(LanguageControl.Get(fName, 12));
                                 }
                                 busyDialog.LargeMessage = LanguageControl.Get(fName, 13);
                                 sourcePath   = "android:SurvivalCraft2.2/WorldUpload.tmp";
                                 needsDelete  = true;
                                 string name2 = WorldsManager.GetWorldInfo(name).WorldSettings.Name;
                                 path         = $"{name2}.scworld";
                                 using (Stream targetStream = Storage.OpenFile(sourcePath, OpenFileMode.Create))
                                 {
                                     WorldsManager.ExportWorld(name, targetStream);
                                 }
                             }
                             busyDialog.LargeMessage = LanguageControl.Get(fName, 14);
                             stream = Storage.OpenFile(sourcePath, OpenFileMode.Read);
                             provider.Upload(path, stream, busyDialog.Progress, delegate(string link)
                             {
                                 long length = stream.Length;
                                 cleanup();
                                 DialogsManager.HideDialog(busyDialog);
                                 if (string.IsNullOrEmpty(link))
                                 {
                                     DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Get("Usual", "success"), string.Format(LanguageControl.Get(fName, 15), DataSizeFormatter.Format(length)), LanguageControl.Get("Usual", "ok"), null, null));
                                 }
                                 else
                                 {
                                     DialogsManager.ShowDialog(null, new ExternalContentLinkDialog(link));
                                 }
                             }, delegate(Exception error)
                             {
                                 cleanup();
                                 DialogsManager.HideDialog(busyDialog);
                                 DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Get("Usual", "error"), error.Message, LanguageControl.Get("Usual", "ok"), null, null));
                             });
                         }
                         catch (Exception ex2)
                         {
                             cleanup();
                             DialogsManager.HideDialog(busyDialog);
                             DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Get("Usual", "error"), ex2.Message, LanguageControl.Get("Usual", "ok"), null, null));
                         }
                     });
                 });
             }
         }
         catch (Exception ex)
         {
             DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Get("Usual", "error"), ex.Message, LanguageControl.Get("Usual", "ok"), null, null));
         }
     }));
 }