/// <summary>
        /// Creates the poll.
        /// </summary>
        /// <returns></returns>
        private PollItem CreatePoll()
        {
            TemplateItem pollTemplate = PollConstants.DatabaseContext.Templates[PollConstants.PollTemplateID];
            string       pollName     = GetItemName(PollingName.Value.Trim()).Trim();
            Item         item;

            if (ParentItem.TemplateID == PollConstants.PollTemplateID)
            {
                item = ParentItem.Parent.Add(pollName, pollTemplate);
                Assert.IsNotNull(item, "Can't create poll item");
                var archivedPoll = new PollItem(ParentItem);
                archivedPoll.ArchiveTo(item);
            }
            else
            {
                item = ParentItem.Add(pollName, pollTemplate);
            }

            PollItem pollItem = new PollItem(item);

            if (pollName != PollingName.Value.Trim())
            {
                pollItem.DisplayName = HttpContext.Current.Server.HtmlEncode(PollingName.Value.Trim());
            }

            pollItem.Intro = string.Format("<h1>{0}</h1>", PollingName.Value.Trim());

            CreateOptions(pollItem);
            return(pollItem);
        }
Example #2
0
        private void CreateLocalDatasource()
        {
            using (new SecurityDisabler())
            {
                TemplateItem template = dbContext.GetTemplate(LocalContent.ID);

                Item newItem = ParentItem.Add("_Local", template);
                if (newItem != null)
                {
                    newItem.Editing.BeginEdit();
                    newItem["__Subitems Sorting"]  = "{C1FF011E-B02A-44E3-8444-9FC89CFC28CE}";
                    newItem.Appearance.DisplayName = "Local Content";
                    newItem.Editing.EndEdit();
                }

                LocalDatasource = newItem;
            }
        }
Example #3
0
        public IActionResult ExtractFilesFromArk([FromBody] ScanRequest request, bool extractMilos, bool extractDTAs, bool convertTextures)
        {
            Archive ark;

            if (!System.IO.File.Exists(request.InputPath))
            {
                // Open as directory if available
                if (Directory.Exists(request.InputPath))
                {
                    ark = ArkFileSystem.FromDirectory(request.InputPath);
                }
                else
                {
                    return(BadRequest($"File \"{request.InputPath}\" does not exist!"));
                }
            }
            else
            {
                // Open as archive
                ark = ArkFile.FromFile(request.InputPath);
            }


            if (request.OutputPath == null)
            {
                return(BadRequest($"Output directory cannot be null!"));
            }

            string CombinePath(string basePath, string path)
            {
                // Consistent slash
                basePath = (request.OutputPath ?? "").Replace("/", "\\");
                path     = (path ?? "").Replace("/", "\\");

                Regex dotRegex = new Regex(@"[.]+[\\]");

                if (dotRegex.IsMatch(path))
                {
                    // Replaces dotdot path
                    path = dotRegex.Replace(path, x => $"({x.Value.Substring(0, x.Value.Length - 1)})\\");
                }

                return(Path.Combine(basePath, path));
            }

            string GetNonGenPath(string path)
            {
                // Consistent slash
                path = (path ?? "").Replace("/", "\\");

                Regex genRegex      = new Regex(@"gen\\[^\\]+$", RegexOptions.IgnoreCase);
                Regex platformRegex = new Regex(@"_[^_]+$", RegexOptions.IgnoreCase); // TODO: Revisit for Forge extensions

                if (genRegex.IsMatch(path))
                {
                    var splitPath = path.Split('\\');
                    var dir       = string.Join("\\", splitPath.SkipLast(2));
                    var file      = platformRegex.Replace(splitPath.Last(), "");

                    path = $"{dir}\\{file}";
                }

                return(path);
            }

            void SaveAsFile(MiloObjectBytes miloEntry, string basePath)
            {
                var fileName = SanitizeFileName(miloEntry.Name);
                var filePath = basePath = Path.Combine(basePath, miloEntry.Type, fileName);

                if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                }

                System.IO.File.WriteAllBytes(filePath, miloEntry.Data);
                Console.WriteLine($"Wrote \"{filePath}\"");
            }

            // Extract everything
            if (!extractDTAs && !extractMilos && !convertTextures)
            {
                foreach (var arkEntry in ark.Entries)
                {
                    var filePath = CombinePath(request.OutputPath, arkEntry.FullPath);

                    if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }

                    using (var fs = System.IO.File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (var stream = ark.GetArkEntryFileStream(arkEntry))
                        {
                            stream.CopyTo(fs);
                        }
                    }

                    Console.WriteLine($"Wrote \"{filePath}\"");
                }

                return(Ok());
            }

            string SanitizeFileName(string fileName)
            {
                // Sanitize file name
                var invalidChars = new Regex($"[{new string(Path.GetInvalidFileNameChars())}]", RegexOptions.IgnoreCase);

                return(invalidChars.Replace(fileName, ""));
            }

            void ProcessMiloArkEntry(ArkEntry miloArkEntry)
            {
                var filePath = CombinePath(request.OutputPath, GetNonGenPath(miloArkEntry.FullPath));

                using (var stream = ark.GetArkEntryFileStream(miloArkEntry))
                {
                    var milo           = MiloFile.ReadFromStream(stream);
                    var miloSerializer = new MiloSerializer(new SystemInfo()
                    {
                        Version   = milo.Version,
                        BigEndian = milo.BigEndian,
                        Platform  = Enum.Parse <Platform>(request.Platform)
                    });

                    var miloDir = new MiloObjectDir();
                    using (var ms = new MemoryStream(milo.Data))
                    {
                        miloSerializer.ReadFromStream(ms, miloDir);
                    }

                    if (convertTextures)
                    {
                        var textureEntries = miloDir.Entries
                                             .Where(x => x.Type == "Tex")
                                             .Select(x => x is Tex ? x as Tex : miloSerializer.ReadFromMiloObjectBytes <Tex>(x as MiloObjectBytes))
                                             .Where(x => x.Bitmap != null && x.Bitmap.RawData?.Length > 0)
                                             .ToList();

                        if (textureEntries.Count <= 0)
                        {
                            return;
                        }

                        foreach (var texEntry in textureEntries)
                        {
                            var entryName = Path.GetFileNameWithoutExtension(SanitizeFileName(texEntry.Name));

                            var pngName = $"{entryName}.png";
                            var pngPath = Path.Combine(filePath, texEntry.Type, pngName);
                            texEntry.Bitmap.SaveAs(miloSerializer.Info, pngPath);

                            Console.WriteLine($"Wrote \"{pngPath}\"");

                            // Write DTA script to file
                            var scriptName = texEntry?.ScriptName ?? "";
                            if (texEntry.Script != null)
                            {
                                var dtaName = (string.IsNullOrEmpty(scriptName))
                                    ? $"{entryName}.dta"
                                    : $"{entryName}_{scriptName}.dta";

                                var dtaPath = Path.Combine(filePath, texEntry.Type, dtaName);

                                var parent = new ParentItem(ParentType.Default);
                                foreach (var item in texEntry.Script.Items)
                                {
                                    parent.Add(item);
                                }

                                texEntry.Script.Items.Clear();
                                texEntry.Script.Items.Add(parent);

                                System.IO.File.WriteAllText(dtaPath, texEntry.Script.ToString());
                                Console.WriteLine($"Wrote \"{dtaPath}\"");
                            }
                        }
                    }

                    if (extractMilos)
                    {
                        if (miloDir.Entries.Count <= 0)
                        {
                            return;
                        }

                        if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        }

                        // Saves milo entries
                        miloDir.Entries.ForEach(x =>
                        {
                            SaveAsFile(x as MiloObjectBytes, filePath);
                            Console.WriteLine($"Wrote \"{filePath}\"");
                        });

                        if (miloDir.Extras.ContainsKey("DirectoryEntry"))
                        {
                            SaveAsFile(miloDir.Extras["DirectoryEntry"] as MiloObjectBytes, filePath);
                            Console.WriteLine($"Wrote \"{filePath}\"");
                        }
                    }
                }
            }

            void ProcessBitmapArkEntry(ArkEntry bitmapArkEntry)
            {
                var arkPath  = GetNonGenPath(bitmapArkEntry.FullPath);
                var filePath = CombinePath(request.OutputPath, $"{Path.GetDirectoryName(arkPath)}\\{Path.GetFileNameWithoutExtension(bitmapArkEntry.FileName)}.png");

                using (var stream = ark.GetArkEntryFileStream(bitmapArkEntry))
                {
                    var miloSerializer = new MiloSerializer(new SystemInfo()
                    {
                        Version   = 25,    // 10 = gh1, 24 = gh2
                        BigEndian = false, // Even on PPC it's LE
                        Platform  = Enum.Parse <Platform>(request.Platform)
                    });

                    var bitmap = miloSerializer.ReadFromStream <HMXBitmap>(stream);
                    bitmap.SaveAs(miloSerializer.Info, filePath);
                    Console.WriteLine($"Wrote \"{filePath}\"");
                }
            }

            // Extract milos
            if (extractMilos || convertTextures)
            {
                var miloEntries = ark.Entries.Where(x => _miloRegex.IsMatch(x.FullPath));

                Parallel.ForEach(miloEntries, (entry) =>
                {
                    ProcessMiloArkEntry(entry);
                });

                //foreach (var entry in miloEntries) ProcessMiloArkEntry(entry);
            }

            // Convert textures
            if (convertTextures)
            {
                var bitmapArkEntries = ark.Entries.Where(x => _bitmapRegex.IsMatch(x.FullPath));

                Parallel.ForEach(bitmapArkEntries, (entry) =>
                {
                    ProcessBitmapArkEntry(entry);
                });

                //foreach (var entry in bitmapArkEntries) ProcessBitmapArkEntry(entry);
            }

            return(Ok());
        }