protected override void InitInternal()
    {
      base.InitInternal();

      //create source and add two files
      source = Context.DownloadFolder.GetFiles().First();

      targetPath = FileSystem.CreateFilePath(Context.EmptyFolder.MetaData.FullName, "target.txt");
    }
 VirtualFile this[string virtualPath]
 {
     get
     {
         lock (key) if (this.virtualFiles.ContainsKey(virtualPath)) return this.virtualFiles[virtualPath];
         var manifestResourceName = virtualPath.Replace("/", ".");
         manifestResourceName = this.manifestResourceNames.FirstOrDefault(n => n.EndsWith(manifestResourceName));
         if (manifestResourceName == null) return null;
         var virtualFile = new VirtualFile(virtualPath, manifestResourceName, this.getManifestResourceStream);
         lock (key) this.virtualFiles.Add(virtualPath, virtualFile);
         return virtualFile;
     }
 }
    protected override void InitInternal()
    {
      var dir = rootDirectory.GetDirectories().First();;
      targetDir = new DirectoryInfo(Path.Combine(rootDirectory.GetDirectories().Last().FullName, "target"));
      targetDir.Create();

      sourcePath = new FileInfo(Path.Combine(dir.FullName, "foo.bin"));
      File.WriteAllBytes(sourcePath.FullName, new byte[99999]);
      sourcePath.Refresh();
      
      targetPath = new FileInfo(Path.Combine(targetDir.FullName, "bar.bin"));
      var fileInfo = provider.GetFileInfo(sourcePath.FullName);
      original = new VirtualFile(provider, fileInfo);
    }
 static bool Prefix(VirtualFile file, ref Texture2D __result)
 {
     GraphicsSettings.LoadTexture(file, ref __result);
     return(false);
 }
        public override Entity Resolve(Entity owner, string key, bool fallback = false)
        {
            TemplateEntity ownerTemplateEntity = TemplateEntity.Decorate(owner);

            if (key == EntityKeys.FieldArpDataTypeKey && owner.Type == EntityKeys.FormatKey)
            {
                return(ResolveArpDataType());
            }
            if (key == EntityKeys.TypeMetaDataFormatKey && owner.Type == EntityKeys.FormatKey)
            {
                return(ResolveTypeMetaDataFormat());
            }
            if (key == EntityKeys.ExpandHiddenTypesFormatKey)
            {
                return(ExpandHiddenTypes());
            }
            if (key == EntityKeys.FilterHiddenTypesFormatKey)
            {
                return(FilterHiddenTypes());
            }
            if (key == EntityKeys.IsFieldKey)
            {
                return(owner.Create(key, CodeEntity.Decorate(owner).AsField != null));
            }
            if (key == EntityKeys.IsTypeKey)
            {
                return(owner.Create(key, CodeEntity.Decorate(owner).AsType != null));
            }
            if (key == EntityKeys.BigDataProjectKey)
            {
                return(GetBigDataProjectEntity());
            }
            if (key == EntityKeys.NormalProjectKey)
            {
                return(GetNormalProjectEntity());
            }


            ISymbol symbol = owner.Value <ISymbol>();

            if (symbol != null)
            {
                Entity result = owner.PropertyValueEntity(key, symbol);
                if (result != null)
                {
                    return(result);
                }

                throw new ContentProviderException(key, owner);
            }

            if (key == EntityKeys.BaseDirectoryKey && HasBaseDirectory(owner, out string baseDirectory))
            {
                return(owner.Create(key, baseDirectory));
            }

            IType type = owner.Value <IType>();

            if (type != null)
            {
                return(ResolveType());
            }

            IField field = owner.Value <IField>();

            if (field != null)
            {
                return(ResolveField());
            }

            IDataType dataType = owner.Value <IDataType>();

            if (dataType != null)
            {
                return(ResolveDataType());
            }

            ICodeModel codeModel = owner.Value <ICodeModel>();

            if (codeModel != null)
            {
                return(ResolveCodeModel());
            }
            throw new ContentProviderException(key, owner);

            IEnumerable <CodeEntity> GetPortEnums()
            {
                return(GetAllPorts().Concat(GetPortStructures().SelectMany(p => p.Fields))
                       .Select(f => f.ResolvedType)
                       .Where(t => t.AsEnum != null)
                       .Distinct());
            }

            IEnumerable <CodeEntity> GetAllPorts()
            {
                bool IsPort(CodeEntity fieldEntity)
                {
                    return(fieldEntity.AsField != null &&
                           fieldEntity.AsField.HasAttributeWithoutValue(EntityKeys.PortAttributeKey));
                }

                return(TemplateEntity.Decorate(owner).EntityHierarchy
                       .Select(CodeEntity.Decorate)
                       .SelectMany(c => c.Fields)
                       .Where(IsPort));
            }

            IEnumerable <CodeEntity> GetPortStructures()
            {
                HashSet <CodeEntity> structures = new HashSet <CodeEntity>(GetAllPorts()
                                                                           .Select(f => f.ResolvedType)
                                                                           .Where(t => t.AsType != null &&
                                                                                  t.AsEnum == null),
                                                                           new FullNameCodeEntityComparer());

                HashSet <CodeEntity> visited = new HashSet <CodeEntity>(new FullNameCodeEntityComparer());

                while (structures.Except(visited).Any())
                {
                    foreach (CodeEntity structure in structures.Except(visited).ToArray())
                    {
                        foreach (CodeEntity structureField in structure.Fields)
                        {
                            CodeEntity structureDataType = structureField.ResolvedType;
                            if (structureDataType.AsType != null && structureDataType.AsEnum == null)
                            {
                                structures.Add(structureDataType);
                            }
                        }

                        visited.Add(structure);
                    }
                }

                return(structures);
            }

            Entity FilterHiddenTypes()
            {
                IEnumerable <IType> types = owner.Select(CodeEntity.Decorate)
                                            .Where(c => !c.IsHidden())
                                            .Select(c => c.AsType)
                                            .ToArray();
                string name = owner.FirstOrDefault()?.Name ?? string.Empty;

                return(owner.Create(key, types.Select(t => owner.Create(name, t.FullName, t))));
            }

            Entity ExpandHiddenTypes()
            {
                IEnumerable <CodeEntity> dataSourceFields = owner.Select(CodeEntity.Decorate);

                dataSourceFields = ExpandHiddenStructureFields(dataSourceFields);
                string name = owner.FirstOrDefault()?.Name ?? string.Empty;

                return(owner.Create(key, dataSourceFields.Select(e => e.AsField)
                                    .Select(f => owner.Create(name, f.Name, f))));

                IEnumerable <CodeEntity> ExpandHiddenStructureFields(IEnumerable <CodeEntity> fields)
                {
                    foreach (CodeEntity hiddenField in fields)
                    {
                        CodeEntity hiddenType = hiddenField.ResolvedType;
                        if (hiddenType == null || !hiddenType.IsHidden())
                        {
                            yield return(hiddenField);

                            continue;
                        }

                        foreach (CodeEntity hiddenTypeField in ExpandHiddenStructureFields(hiddenType.Fields))
                        {
                            yield return(hiddenTypeField);
                        }
                    }
                }
            }

            T ChooseMetaDataTemplate <T>(IEnumerable <(T template, string name, string context)> templates)
            {
                return(templates.OrderByDescending(t => ContextComplexity(t.context ?? string.Empty))
                       .FirstOrDefault(Matches)
                       .template);

                bool Matches((T template, string name, string context) template)
                {
                    if (!key.Equals(template.name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(false);
                    }

                    if (string.IsNullOrEmpty(template.context))
                    {
                        return(true);
                    }
                    string[] contextPath = template.context.Split(new [] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries)
                                           .Reverse().ToArray();
                    Entity current = owner;

                    foreach (string part in contextPath)
                    {
                        current = current?.Owner;
                        if (current?[$"is{part}"].Value <bool>() != true)
                        {
                            return(false);
                        }
                    }

                    return(true);
                }

                int ContextComplexity(string context)
                {
                    return(context.Count(c => c == '/' || c == '\\'));
                }
            }

            Entity ResolveType()
            {
                switch (key)
                {
                case EntityKeys.PathKey:
                {
                    return(owner.Create(key, type.GetFile(owner).Parent.FullName));
                }

                case EntityKeys.FieldsKey:
                {
                    return(owner.Create(key, type.Fields.Select(f => owner.Create(key.Singular(), f.Name, f))));
                }

                case EntityKeys.FileKey:
                {
                    VirtualFile file = owner.Root.Value <ICodeModel>()
                                       .Types[type];
                    return(owner.Create(key, file.Name, file));
                }

                case EntityKeys.BaseTypeKey:
                {
                    IDataType baseType = type.BaseTypes.FirstOrDefault();
                    return(owner.Create(key, baseType));
                }

                default:
                {
                    if (TryResolveTypeTemplate(out Entity result))
                    {
                        return(result);
                    }

                    if (TryResolveProperty(out result))
                    {
                        return(result);
                    }

                    if (TryResolveFields(out result))
                    {
                        return(result);
                    }

                    if (TryResolveRelationship(out result))
                    {
                        return(result);
                    }

                    throw new ContentProviderException(key, owner);
                }
                }

                bool TryResolveTypeTemplate(out Entity result)
                {
                    Templates.Type.metaDataTemplate typeMetaDataTemplate = ChooseMetaDataTemplate(templateRepository.TypeTemplates
                                                                                                  .Select(t => (t, t.name, t.context)));
                    if (typeMetaDataTemplate != null)
                    {
                        result = GetTypeTemplateEntity(typeMetaDataTemplate);
                        return(true);
                    }

                    result = null;
                    return(false);

                    Entity GetTypeTemplateEntity(Templates.Type.metaDataTemplate metaDataTemplate)
                    {
                        if (!metaDataTemplate.hasvalue)
                        {
                            bool hasAttribute = type.HasAttributeWithoutValue(metaDataTemplate.name);
                            return(owner.Create(key, new Func <string>(hasAttribute.ToString), hasAttribute));
                        }
                        (string[] values, CodePosition position) = GetTypeTemplateValue();
                        IEnumerable <string> formattedValues = VerifyValues().ToArray();

                        return(metaDataTemplate.multiplicity == Templates.Type.multiplicity.OneOrMore
                                   ? owner.Create(key, formattedValues.Select(v => owner.Create(key.Singular(), v)))
                                   : owner.Create(key, formattedValues.First()));

                        IEnumerable <string> VerifyValues()
                        {
                            return(values.Select(VerifyValue));

                            string VerifyValue(string arg)
                            {
                                (bool success, string message, string newValue) = metaDataTemplate.ValueRestriction.Verify(arg);
                                if (!success)
                                {
                                    owner.AddCodeException(new FieldAttributeRestrictionException(metaDataTemplate.name, arg, message, position, type.GetFile(owner)));
                                }

                                return(newValue);
                            }
                        }

                        (string[], CodePosition) GetTypeTemplateValue()
                        {
                            IAttribute attribute = type.Attributes.LastOrDefault(a => a.Name.Equals(metaDataTemplate.name,
                                                                                                    StringComparison.OrdinalIgnoreCase));
                            string value = attribute?.Values.FirstOrDefault() ??
                                           resolver.Resolve(metaDataTemplate.defaultvalue, owner);

                            return(metaDataTemplate.multiplicity == Templates.Type.multiplicity.OneOrMore
                                        ? value.Split(new[] { metaDataTemplate.split }, StringSplitOptions.RemoveEmptyEntries)
                                        : new[] { value },
                                   attribute?.Position ?? new CodePosition(0, 0));
                        }
                    }
                }

                bool TryResolveProperty(out Entity result)
                {
                    result = owner.PropertyValueEntity(key, type);
                    return(result != null);
                }

                bool TryResolveFields(out Entity result)
                {
                    metaDataTemplate metaDataTemplate = templateRepository.FieldTemplates
                                                        .FirstOrDefault(t => t.name.Plural().Equals(key,
                                                                                                    StringComparison
                                                                                                    .OrdinalIgnoreCase));

                    if (metaDataTemplate != null)
                    {
                        IEnumerable <IField> fields = type.Fields
                                                      .Where(f => f.Attributes
                                                             .Any(a => a.Name
                                                                  .Equals(metaDataTemplate.name,
                                                                          StringComparison.OrdinalIgnoreCase)));
                        result = owner.Create(key, fields.Select(f => owner.Create(metaDataTemplate.name, f.Name, f)));
                        return(true);
                    }

                    result = null;
                    return(false);
                }

                bool TryResolveRelationship(out Entity result)
                {
                    templateRelationship[] relationships = owner.Template().Relationship;
                    templateRelationship   relationship  = relationships?.FirstOrDefault(r => r.name.Equals(key, StringComparison.OrdinalIgnoreCase))
                                                           ?? relationships?.FirstOrDefault(r => r.name.Equals(key.Singular(), StringComparison.OrdinalIgnoreCase));
                    TemplateDescription relationshipDescription = relationship != null?templateRepository.Template(relationship.type) : null;

                    if (relationshipDescription != null)
                    {
                        IEnumerable <string> names = type.Attributes
                                                     .Where(a => a.Name.Equals(relationship.name,
                                                                               StringComparison.OrdinalIgnoreCase))
                                                     .SelectMany(a => a.Values);
                        result = relationship.GetRelationship(relationshipDescription, owner, names.ToArray());
                        return(true);
                    }

                    result = null;
                    return(false);
                }
            }

            Entity ResolveField()
            {
                switch (key)
                {
                case EntityKeys.FieldNameKey:
                {
                    return(owner.Create(key, field.Name));
                }

                case EntityKeys.DataTypeKey:
                {
                    IDataType fieldDataType = GetRealFieldDataType();
                    return(owner.Create(key, fieldDataType.Name, fieldDataType));
                }

                case EntityKeys.ResolvedTypeKey:
                {
                    ICodeModel model         = owner.Root.Value <ICodeModel>();
                    IDataType  fieldDataType = field.DataType;
                    IType      realType      = fieldDataType.PotentialFullNames
                                               .Select(model.Type)
                                               .FirstOrDefault(t => t != null);
                    return(owner.Create(key, realType?.Name, realType));
                }

                case EntityKeys.MultiplicityKey:
                {
                    return(owner.Create(key, field.Multiplicity.Select(m => owner.Create(key, new Func <string>(m.ToString), m))));
                }

                default:
                {
                    metaDataTemplate metaDataTemplate = templateRepository.FieldTemplates
                                                        .FirstOrDefault(t => t.name.Equals(key,
                                                                                           StringComparison.OrdinalIgnoreCase));
                    if (metaDataTemplate != null)
                    {
                        return(GetFieldTemplateEntity(metaDataTemplate));
                    }
                    throw new ContentProviderException(key, owner);
                }
                }

                IDataType GetRealFieldDataType()
                {
                    ICodeModel rootCodeModel = owner.Root.Value <ICodeModel>();
                    IEnum      @enum         = field.DataType.PotentialFullNames.Select(n => rootCodeModel.GetEnum(n)).FirstOrDefault(e => e != null);

                    if (@enum != null)
                    {
                        (IDataType enumBaseType, _) = GetEnumBaseType(@enum);
                        return(enumBaseType);
                    }

                    return(field.DataType);
                }

                Entity GetFieldTemplateEntity(metaDataTemplate metaDataTemplate)
                {
                    if (!metaDataTemplate.hasvalue)
                    {
                        bool hasAttribute = field.HasAttributeWithoutValue(metaDataTemplate.name);
                        return(owner.Create(key, new Func <string>(hasAttribute.ToString), hasAttribute));
                    }
                    (string[] values, CodePosition position) = GetFieldTemplateValue();
                    IEnumerable <string> formattedValues = VerifyValues().ToArray();

                    return(metaDataTemplate.multiplicity == multiplicity.OneOrMore
                               ? owner.Create(key, formattedValues.Select(v => owner.Create(key.Singular(), v)))
                               : owner.Create(key, formattedValues.First()));

                    IEnumerable <string> VerifyValues()
                    {
                        return(values.Select(VerifyValue));

                        string VerifyValue(string arg)
                        {
                            (bool success, string message, string newValue) = metaDataTemplate.ValueRestriction.Verify(arg);
                            if (!success)
                            {
                                owner.AddCodeException(new FieldAttributeRestrictionException(metaDataTemplate.name, arg, message, position, field.GetFile(owner)));
                            }

                            return(newValue);
                        }
                    }

                    (string[], CodePosition) GetFieldTemplateValue()
                    {
                        IAttribute attribute = field.Attributes.LastOrDefault(a => a.Name.Equals(metaDataTemplate.name,
                                                                                                 StringComparison.OrdinalIgnoreCase));
                        string value = string.IsNullOrEmpty(attribute?.Values.FirstOrDefault())
                                           ? resolver.Resolve(metaDataTemplate.defaultvalue, owner)
                                           : attribute.Values.First();

                        return(metaDataTemplate.multiplicity == multiplicity.OneOrMore
                                    ? value.Split(new[] { metaDataTemplate.split }, StringSplitOptions.RemoveEmptyEntries)
                                    : new[] { value },
                               attribute?.Position ?? new CodePosition(0, 0));
                    }
                }
            }

            Entity ResolveCodeModel()
            {
                switch (key)
                {
                case EntityKeys.PortStructsKey:
                {
                    return(owner.Create(key, GetPortStructures().Select(c => c.Base)));
                }

                case EntityKeys.PortEnumsKey:
                {
                    return(owner.Create(key, GetPortEnums().Select(c => c.Base)));
                }

                case EntityKeys.NamespaceKey:
                {
                    bool prior206Target = CheckProjectTargets();
                    IEnumerable <string> relevantTypes = TemplateEntity.Decorate(owner).EntityHierarchy
                                                         .Select(CodeEntity.Decorate)
                                                         .Where(c => !c.IsRoot())
                                                         .Select(c => c.FullName);
                    string ns = prior206Target
                                        ? codeModel.RootNamespaceForOldTarget(
                        relevantTypes.ToArray(), GetPortStructures().Concat(GetPortEnums())
                        .Select(c => c.FullName)
                        .ToArray())
                                        : codeModel.RootNamespace(relevantTypes.ToArray());
                    if (string.IsNullOrEmpty(ns))
                    {
                        ns = owner.Name;
                    }

                    return(owner.Create(key, ns));
                }

                default:
                    throw new ContentProviderException(key, owner);
                }

                bool CheckProjectTargets()
                {
                    IEnumerable <TargetEntity> targets = ProjectEntity.Decorate(owner).Targets.Select(t => TargetEntity.Decorate(t));

                    return(targets.Any(t => t.Version < new Version(20, 6)));
                }
            }

            Entity ResolveDataType()
            {
                switch (key)
                {
                case EntityKeys.NameKey:
                    return(owner.Create(key, dataType.Name));

                case EntityKeys.FullNameKey:
                    string fullName = GetDataTypeFullName();
                    return(owner.Create(key, fullName));

                default:
                    throw new ContentProviderException(key, owner);
                }

                string GetDataTypeFullName()
                {
                    ICodeModel rootCodeModel = owner.Root.Value <ICodeModel>();

                    return(dataType.PotentialFullNames.FirstOrDefault(fn => rootCodeModel.Type(fn) != null)
                           ?? dataType.Name);
                }
            }

            Entity ResolveTypeMetaDataFormat()
            {
                IEntityBase dataSource         = ownerTemplateEntity.FormatOrigin;
                IDataType   dataSourceDataType = dataSource.HasValue <IDataType>()
                                                   ? dataSource.Value <IDataType>()
                                                   : dataSource.Value <IField>().DataType;

                ICodeModel rootCodeModel = dataSource.Root.Value <ICodeModel>();
                string     dataTypeName  = string.Empty;

                if (dataSourceDataType != null)
                {
                    dataTypeName = dataSourceDataType.PotentialFullNames
                                   .Select(n => rootCodeModel.Type(n))
                                   .FirstOrDefault(t => t != null)
                                   ?.Name
                                   ?? datatypeConversion.Convert(dataSourceDataType);
                }

                return(owner.Create(key, dataTypeName));
            }

            Entity ResolveArpDataType()
            {
                IEntityBase dataSource      = ownerTemplateEntity.FormatOrigin;
                IField      dataSourceField = dataSource.Value <IField>();

                if (dataSourceField == null)
                {
                    throw new FormatTargetMismatchException("arpDataType", "field|port", dataSource.Type);
                }

                ICodeModel rootCodeModel = dataSource.Root.Value <ICodeModel>();
                bool       isStruct      = dataSourceField.DataType.PotentialFullNames.Any(n => rootCodeModel.GetClass(n) != null || rootCodeModel.GetStructure(n) != null);
                IEnum      @enum         = dataSourceField.DataType.PotentialFullNames.Select(n => rootCodeModel.GetEnum(n)).FirstOrDefault(e => e != null);
                bool       isArray       = dataSourceField.Multiplicity.Any();
                string     arpName       = "DataType::" + GetArpDataType(dataSourceField.DataType.Name);

                return(owner.Create(key, arpName));

                string GetArpDataType(string dataTypeName)
                {
                    string postfix = isArray ? " | DataType::Array" : string.Empty;

                    if (isStruct)
                    {
                        return("Struct" + postfix);
                    }

                    if (@enum != null)
                    {
                        (IDataType enumBaseType, string formattedBaseType) = GetEnumBaseType(@enum);

                        return($"Enum | DataType::{formattedBaseType}" + postfix);
                    }

                    (bool success, string value) = FormatDataType(dataTypeName);

                    if (!success)
                    {
                        throw new UnknownDataTypeException(value);
                    }

                    return(value + postfix);
                }
            }

            (bool success, string value) FormatDataType(string unformattedValue)
            {
                if (unformattedValue.Contains('<'))
                {
                    unformattedValue = unformattedValue.Substring(0, unformattedValue.IndexOf('<'));
                }

                string result      = owner.Create("temporaryCtnFormatContainer", unformattedValue).Format()["knownDataTypes"].Value <string>();
                Match  unkownMatch = UnkownDataTypeRegex.Match(result);

                if (unkownMatch.Success)
                {
                    return(false, unkownMatch.Groups["dataType"].Value);
                }

                return(true, result);
            }

            (IDataType, string) GetEnumBaseType(IEnum @enum)
            {
                IDataType enumDataType = @enum.BaseTypes.FirstOrDefault();
                string    enumBaseType = enumDataType?.Name;

                if (string.IsNullOrEmpty(enumBaseType))
                {
                    throw new MissingEnumDataTypeException(@enum);
                }

                (bool formatted, string formattedBaseType) = FormatDataType(enumBaseType);
                if (!formatted)
                {
                    throw new UnkownEnumDataTypeException(formattedBaseType, @enum);
                }

                return(enumDataType, formattedBaseType);
            }

            Entity GetBigDataProjectEntity()
            {
                if (HasMoreThan1000Fields())
                {
                    return(owner.Create(key, "true", true));
                }
                return(owner.Create(key, "false", false));
            }

            Entity GetNormalProjectEntity()
            {
                if (HasMoreThan1000Fields())
                {
                    return(owner.Create(key, "false", false));
                }
                return(owner.Create(key, "true", true));
            }

            bool HasMoreThan1000Fields()
            {
                ICodeModel model = owner.Value <ICodeModel>();

                if (model == null ||
                    GetAllPorts().Concat(GetPortStructures().SelectMany(s => s.Fields)).Count() <= 1000)
                {
                    return(false);
                }
                return(true);
            }
        }
示例#6
0
        public EngineResult Execute()
        {
            try {
                _logger.Info("Initializing virtual filesystem");

                var         mapStream = File.Open(_settings.InputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                VirtualFile vmapFile;
                var         mixMap = new MixFile(mapStream, _settings.InputFile, 0, mapStream.Length, false, false);
                if (mixMap.IsValid())                   // input max is a mix
                {
                    var mapArchive = new MixFile(mapStream, Path.GetFileName(_settings.InputFile), true);
                    // grab the largest file in the archive
                    var mixEntry = mapArchive.Index.OrderByDescending(me => me.Value.Length).First();
                    vmapFile = mapArchive.OpenFile(mixEntry.Key);
                }
                else
                {
                    vmapFile = new VirtualFile(mapStream, Path.GetFileName(_settings.InputFile), true);
                }
                var mapFile = new MapFile(vmapFile, Path.GetFileName(_settings.InputFile));

                ModConfig modConfig = null;
                if (!string.IsNullOrEmpty(_settings.ModConfig))
                {
                    if (File.Exists(_settings.ModConfig))
                    {
                        ModConfig cfg;
                        try {
                            using (FileStream f = File.OpenRead(_settings.ModConfig))
                                modConfig = ModConfig.Deserialize(f);
                        }
                        catch (IOException) {
                            _logger.Fatal("IOException while loading mod config");
                        }
                        catch (XmlException) {
                            _logger.Fatal("XmlException while loading mod config");
                        }
                        catch (SerializationException) {
                            _logger.Fatal("Serialization exception while loading mod config");
                        }
                    }
                    else
                    {
                        _logger.Fatal("Invalid mod config file specified");
                    }
                }

                if (_settings.Engine == EngineType.AutoDetect)
                {
                    _settings.Engine = EngineDetector.DetectEngineType(mapFile);
                    _logger.Info("Engine autodetect result: {0}", _settings.Engine);
                }

                // Engine type is now definitive, load mod config
                if (modConfig == null)
                {
                    modConfig = ModConfig.GetDefaultConfig(_settings.Engine);
                }

                var map = new Map.Map {
                    IgnoreLighting  = _settings.IgnoreLighting,
                    StartPosMarking = _settings.StartPositionMarking,
                    StartMarkerSize = _settings.MarkerStartSize,
                    MarkOreFields   = _settings.MarkOreFields
                };

                using (var vfs = new VirtualFileSystem()) {
                    // first add the dirs, then load the extra mixes, then scan the dirs
                    foreach (string modDir in modConfig.Directories)
                    {
                        vfs.Add(modDir);
                    }

                    // add mixdir to VFS (if it's not included in the mod config)
                    if (!modConfig.Directories.Any())
                    {
                        string mixDir =
                            VirtualFileSystem.DetermineMixDir(_settings.MixFilesDirectory, _settings.Engine);
                        vfs.Add(mixDir);
                    }

                    foreach (string mixFile in modConfig.ExtraMixes)
                    {
                        vfs.Add(mixFile);
                    }

                    vfs.LoadMixes(_settings.Engine);

                    if (!map.Initialize(mapFile, modConfig, vfs))
                    {
                        _logger.Error("Could not successfully load this map. Try specifying the engine type manually.");
                        return(EngineResult.LoadRulesFailed);
                    }

                    if (!map.LoadTheater())
                    {
                        _logger.Error("Could not successfully load all required components for this map. Aborting.");
                        return(EngineResult.LoadTheaterFailed);
                    }

                    if (_settings.MarkStartPos && _settings.StartPositionMarking == StartPositionMarking.Tiled)
                    {
                        map.MarkTiledStartPositions();
                    }

                    if (_settings.MarkOreFields)
                    {
                        map.MarkOreAndGems();
                    }

                    if ((_settings.GeneratePreviewPack || _settings.FixupTiles || _settings.FixOverlays ||
                         _settings.CompressTiles) && _settings.Backup)
                    {
                        if (mapFile.BaseStream is MixFile)
                        {
                            _logger.Error("Cannot generate a map file backup into an archive (.mmx/.yro/.mix)!");
                        }
                        else
                        {
                            try {
                                string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
                                string fileInput = Path.Combine(Path.GetDirectoryName(_settings.InputFile),
                                                                Path.GetFileName(_settings.InputFile));
                                fileInput = fileInput.TrimEnd(Path.DirectorySeparatorChar,
                                                              Path.AltDirectorySeparatorChar);
                                string fileInputNoExtn = Path.Combine(Path.GetDirectoryName(_settings.InputFile),
                                                                      Path.GetFileNameWithoutExtension(_settings.InputFile));
                                fileInputNoExtn = fileInputNoExtn.TrimEnd(Path.DirectorySeparatorChar,
                                                                          Path.AltDirectorySeparatorChar);
                                string fileBackup = fileInputNoExtn + "_" + timestamp + ".bkp";
                                File.Copy(fileInput, fileBackup, true);
                                _logger.Info("Creating map backup: " + fileBackup);
                            }
                            catch (Exception) {
                                _logger.Error("Unable to generate a map file backup!");
                            }
                        }
                    }

                    if (_settings.FixupTiles)
                    {
                        map.FixupTileLayer();
                    }

                    map.Draw();

                    if (_settings.MarkIceGrowth)
                    {
                        map.MarkIceGrowth();
                    }

                    if (_settings.TunnelPaths)
                    {
                        map.PlotTunnels(_settings.TunnelPosition);
                    }

                    if (_settings.MarkStartPos && (_settings.StartPositionMarking == StartPositionMarking.Squared ||
                                                   _settings.StartPositionMarking == StartPositionMarking.Circled ||
                                                   _settings.StartPositionMarking == StartPositionMarking.Diamond ||
                                                   _settings.StartPositionMarking == StartPositionMarking.Ellipsed ||
                                                   _settings.StartPositionMarking == StartPositionMarking.Starred))
                    {
                        map.DrawStartPositions();
                    }

                    if (_settings.OutputFile == "")
                    {
                        _settings.OutputFile = DetermineMapName(mapFile, _settings.Engine, vfs);
                    }

                    if (_settings.OutputDir == "")
                    {
                        _settings.OutputDir = Path.GetDirectoryName(_settings.InputFile);
                    }
                }                 // VFS resources can now be released

                if (_settings.DiagnosticWindow)
                {
                    using (var form = new DebugDrawingSurfaceWindow(map.GetDrawingSurface(), map.GetTiles(),
                                                                    map.GetTheater(), map)) {
                        form.RequestTileEvaluate += map.DebugDrawTile;
                        form.ShowDialog();
                    }
                }

                // free up as much memory as possible before saving the large images
                Rectangle      saveRect = map.GetSizePixels(_settings.SizeMode);
                DrawingSurface ds       = map.GetDrawingSurface();
                saveRect.Intersect(new Rectangle(0, 0, ds.Width, ds.Height));
                // if we don't need this data anymore, we can try to save some memory
                if (!_settings.GeneratePreviewPack)
                {
                    ds.FreeNonBitmap();
                    map.FreeUseless();
                    GC.Collect();
                }

                if (_settings.SaveJPEG)
                {
                    ds.SaveJPEG(Path.Combine(_settings.OutputDir, _settings.OutputFile + ".jpg"),
                                _settings.JPEGCompression, saveRect);
                }

                if (_settings.SavePNG)
                {
                    ds.SavePNG(Path.Combine(_settings.OutputDir, _settings.OutputFile + ".png"),
                               _settings.PNGQuality, saveRect);
                }

                Regex reThumb = new Regex(@"(\+|)?\((\d+),(\d+)\)");
                var   match   = reThumb.Match(_settings.ThumbnailConfig);
                if (match.Success)
                {
                    Size dimensions = new Size(
                        int.Parse(match.Groups[2].Captures[0].Value),
                        int.Parse(match.Groups[3].Captures[0].Value));
                    var cutRect = map.GetSizePixels(_settings.SizeMode);

                    if (match.Groups[1].Captures[0].Value == "+")
                    {
                        // + means maintain aspect ratio

                        if (dimensions.Width > 0 && dimensions.Height > 0)
                        {
                            float scaleHeight = (float)dimensions.Height / (float)cutRect.Height;
                            float scaleWidth  = (float)dimensions.Width / (float)cutRect.Width;
                            float scale       = Math.Min(scaleHeight, scaleWidth);
                            dimensions.Width  = Math.Max((int)(cutRect.Width * scale), 1);
                            dimensions.Height = Math.Max((int)(cutRect.Height * scale), 1);
                        }
                        else
                        {
                            double aspectRatio = cutRect.Width / (double)cutRect.Height;
                            if (dimensions.Width / (double)dimensions.Height > aspectRatio)
                            {
                                dimensions.Height = (int)(dimensions.Width / aspectRatio);
                            }
                            else
                            {
                                dimensions.Width = (int)(dimensions.Height * aspectRatio);
                            }
                        }
                    }

                    _logger.Info("Saving thumbnail with dimensions {0}x{1}", dimensions.Width, dimensions.Height);

                    if (!_settings.SavePNGThumbnails)
                    {
                        ds.SaveThumb(dimensions, cutRect,
                                     Path.Combine(_settings.OutputDir, "thumb_" + _settings.OutputFile + ".jpg"));
                    }
                    else
                    {
                        ds.SaveThumb(dimensions, cutRect,
                                     Path.Combine(_settings.OutputDir, "thumb_" + _settings.OutputFile + ".png"), true);
                    }
                }

                if (_settings.GeneratePreviewPack || _settings.FixupTiles || _settings.FixOverlays ||
                    _settings.CompressTiles)
                {
                    if (mapFile.BaseStream is MixFile)
                    {
                        _logger.Error(
                            "Cannot fix tile layer or inject thumbnail into an archive (.mmx/.yro/.mix)!");
                    }
                    else
                    {
                        if (_settings.GeneratePreviewPack)
                        {
                            map.GeneratePreviewPack(_settings.PreviewMarkers, _settings.SizeMode, mapFile,
                                                    _settings.FixPreviewDimensions);
                        }

                        if (_settings.FixOverlays)
                        {
                            map.FixupOverlays();                             // fixing is done earlier, it now creates overlay and its data pack
                        }
                        // Keep this last in tiles manipulation
                        if (_settings.CompressTiles)
                        {
                            map.CompressIsoMapPack5();
                        }

                        _logger.Info("Saving map to " + _settings.InputFile);
                        mapFile.Save(_settings.InputFile);
                    }
                }
            }
            catch (Exception exc) {
                _logger.Error(string.Format("An unknown fatal exception occurred: {0}", exc), exc);
#if DEBUG
                throw;
#endif
                return(EngineResult.Exception);
            }
            return(EngineResult.RenderedOk);
        }
示例#7
0
 public LoadedContentItem(VirtualFile internalFile, T contentItem, IDisposable extraDisposable = null)
 {
     this.internalFile    = internalFile;
     this.contentItem     = contentItem;
     this.extraDisposable = extraDisposable;
 }
示例#8
0
 public bool accepts(VirtualFile file)
 {
     return(file.File && ProcessApplicationScanningUtil.isDeployable(file.Name, additionalResourceSuffixes));
 }
示例#9
0
 public bool accepts(VirtualFile file)
 {
     return(ProcessApplicationScanningUtil.isDiagram(file.Name, process.Name));
 }
示例#10
0
        /**
         * Attempts to include the given file.
         *
         * User code may override this method to implement a virtual
         * file system.
         */
        private bool include(VirtualFile file, bool isImport, bool checkOnly)
        {
            // System.out.println("Try to include " + file);
            if (!file.isFile())
            return false;

            if(!checkOnly) {
            if(isImport) {
                if(_importedPaths.Contains(file.getPath())) {
                    return true;
                }

                _importedPaths.Add(file.getPath());
            }

            if(getFeature(Feature.DEBUG))
                System.Console.WriteLine("pp: including " + file);

            push_source(file.getSource(), true);
            }
            return true;
        }
        /// <summary>
        /// For internal use only
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <returns></returns>
        public override VirtualFile GetFile(string virtualPath)
        {
            VirtualFile vf = (VirtualFile)GetFile(virtualPath, null);

            return((vf == null) ? Previous.GetFile(virtualPath) : vf);
        }
示例#12
0
文件: Data.cs 项目: kman012/wabbajack
 public RawSourceFile(VirtualFile file)
 {
     File = file;
 }
示例#13
0
        private Settings LoadSettings()
        {
            string path = SettingsPath;

            if (!fileSystem.FileExists(path))
            {
                log.LogInformation("No settings file found. Use default settings:");
                Settings result = new Settings();
                log.LogInformation(JsonConvert.SerializeObject(result, Formatting.Indented));
                return(result);
            }
            VirtualFile file = fileSystem.GetFile(path);

            using (Stream stream = file.OpenRead())
            {
                try
                {
                    XDocument            document = XDocument.Load(stream);
                    IEnumerable <string> paths    = document.Descendants("SDKS").Any()
                                                    ? document.Descendants("SDK").Select(d => d.Value)
                                                    : Settings.SdkPathsDefault;
                    IEnumerable <string> templates = document.Descendants("Templates").Any()
                                                        ? document.Descendants("Template").Select(d => d.Value)
                                                        : Settings.TemplateLocationsDefault;
                    string attributePrefix = document.Descendants("Settings").FirstOrDefault()?
                                             .Attribute("AttributePrefix")?.Value
                                             ?? Settings.AttributePrefixDefault;
                    string cliRpositoryRoot = document.Descendants("Settings").FirstOrDefault()?
                                              .Element("CliRepositoryRoot")?.Value
                                              ?? Settings.CliRepositoryRootDefault;
                    string cliRpositoryFileName = document.Descendants("Settings").FirstOrDefault()?
                                                  .Element("CliRepositoryFileName")?.Value
                                                  ?? Settings.CliRepositoryFileNameDefault;
                    string cliRpositorySignatureFileName = document.Descendants("Settings").FirstOrDefault()?
                                                           .Element("CliRepositorySignatureFileName")?.Value
                                                           ?? Settings.CliRepositorySignatureFileNameDefault;
                    string httpProxy = document.Descendants("Settings").FirstOrDefault()?
                                       .Element("HttpProxy")?.Value
                                       ?? Settings.HttpProxyDefault;
                    string logFilePath = document.Descendants("Settings").FirstOrDefault()?
                                         .Element("LogFilePath")?.Value
                                         ?? Settings.LogFilePathDefault;
                    bool systemCommands = bool.TryParse(document.Descendants("Settings").FirstOrDefault()?
                                                        .Element("SystemCommands")?.Value, out bool parsed)
                                              ? parsed
                                              : Settings.UseSystemCommandsDefault;
                    bool extendedLog = bool.TryParse(document.Descendants("Settings").FirstOrDefault()?
                                                     .Element(nameof(Settings.AlwaysWriteExtendedLog))?.Value, out parsed)
                                              ? parsed
                                              : Settings.AlwaysWriteExtendedLogDefault;

                    log.LogInformation($"Used settings {file.FullName}:");
                    Settings result = new Settings(attributePrefix, paths.ToArray(), cliRpositoryRoot, cliRpositoryFileName, cliRpositorySignatureFileName, httpProxy, logFilePath, templates.ToArray(), systemCommands, extendedLog);
                    log.LogInformation(JsonConvert.SerializeObject(result, Formatting.Indented));
                    return(result);
                }catch (System.Xml.XmlException e)
                {
                    throw new FormattableException($"XML error in settings file {path}.", e);
                }
            }
        }
示例#14
0
        public async Task Unpack(VirtualFile file, VirtualDirectory destination,
                                 IProgressNotifier parentProgress = null, ChangeObservable observable = null)
        {
            if (Path.GetExtension(file.Name)?.Equals(".zip", StringComparison.OrdinalIgnoreCase) == true)
            {
                await UnpackZipFile().ConfigureAwait(false);
            }
            else if (file.Name.EndsWith(".tar.xz", StringComparison.OrdinalIgnoreCase))
            {
                UnpackTarXzFile(file);
            }
            else if (Path.GetExtension(file.Name)?.Equals(".xz", StringComparison.OrdinalIgnoreCase) == true)
            {
                UnpackXzFile(file);
            }
            else if (Path.GetExtension(file.Name)?.Equals(".tar", StringComparison.OrdinalIgnoreCase) == true)
            {
                UnpackTarFile(file);
            }
            else if (Path.GetExtension(file.Name)?.Equals(".sh", StringComparison.OrdinalIgnoreCase) == true &&
                     environmentService.Platform != OSPlatform.Windows)
            {
                await UnpackSelfExtractingShellScript().ConfigureAwait(false);
            }
            else
            {
                throw new UnsupportedArchiveFormatException(file.FullName);
            }

            async Task UnpackSelfExtractingShellScript()
            {
                ValidateShellScript();
                StringBuilderUserInterface userInterface = new StringBuilderUserInterface(log, writeInformation: true, writeError: true);

                try
                {
                    using (parentProgress?.SpawnInfiniteProgress("Executing the shell script."))
                        using (IProcess process = processManager.StartProcess(file.FullName, $"-y -d \"{destination.FullName}\"", userInterface))
                        {
                            await process.WaitForExitAsync().ConfigureAwait(false);

                            if (process.ExitCode != 0)
                            {
                                throw new UnsupportedArchiveFormatException(file.FullName,
                                                                            new FormattableException(
                                                                                $"An error occured while executing the script.{Environment.NewLine}" +
                                                                                $"{userInterface.Error}"));
                            }
                        }
                }
                catch (Exception e)
                {
                    if (e is FormattableException)
                    {
                        throw;
                    }
                    throw new UnsupportedArchiveFormatException(file.FullName,
                                                                new FormattableException(
                                                                    $"An exception occured while executing the script.{Environment.NewLine}" +
                                                                    $"{e.Message}", e));
                }

                void ValidateShellScript()
                {
                    StringBuilderUserInterface validationUserInterface = new StringBuilderUserInterface(log, writeInformation: true, writeError: true);

                    try
                    {
                        using (IProcess process = processManager.StartProcess(file.FullName, "--help", validationUserInterface))
                        {
                            process.WaitForExit();
                        }
                    }
                    catch (UnauthorizedAccessException e)
                    {
                        throw new UnsupportedArchiveFormatException(file.FullName,
                                                                    new FormattableException(
                                                                        $"An exception occured while inspecting the script.{Environment.NewLine}" +
                                                                        $"This excpetion can occur when the file is not marked as executable.{Environment.NewLine}" +
                                                                        $"{e.Message}", e));
                    }
                    catch (Exception e)
                    {
                        if (e is FormattableException)
                        {
                            throw;
                        }
                        throw new UnsupportedArchiveFormatException(file.FullName,
                                                                    new FormattableException(
                                                                        $"An exception occured while inspecting the script.{Environment.NewLine}" +
                                                                        $"{e.Message}", e));
                    }

                    if (!Regex.IsMatch(validationUserInterface.Information,
                                       @"(?=.*(?:usage|Usage)).*(?=.*-y)(?=.*-d)"))
                    {
                        throw new UnsupportedArchiveFormatException(file.FullName,
                                                                    new FormattableException($"Did not find the expected usage information.{Environment.NewLine}" +
                                                                                             $"The expected information need to include the options '-y' and '-d'.{Environment.NewLine}" +
                                                                                             $"The following usage information was given:{Environment.NewLine}" +
                                                                                             $"{validationUserInterface.Information}"));
                    }
                }
            }

            async Task UnpackZipFile()
            {
                using (Stream fileStream = file.OpenRead())
                    using (ZipFile zipFile = new ZipFile(fileStream))
                        using (IProgressNotifier mainProgress = parentProgress?.Spawn(2, $"Extracting {file.FullName} to {destination.FullName}."))
                        {
                            archiveResultBuilder.Clear();
                            using (mainProgress?.SpawnInfiniteProgress("Test archive integrity."))
                            {
                                ZipFile copy = zipFile;
                                await Task.Run(() =>
                                {
                                    if (!copy.TestArchive(true, TestStrategy.FindAllErrors, ResultHandler))
                                    {
                                        throw new UnsupportedArchiveFormatException(
                                            file.FullName, new FormattableException(archiveResultBuilder.ToString()));
                                    }
                                }).ConfigureAwait(false);
                            }

                            double increment = (double)Constants.ProgressMaxResolution / zipFile.Count + 1;
                            using (IProgressNotifier extractProgress = mainProgress?.Spawn(Constants.ProgressMaxResolution, "Extract files"))
                            {
                                foreach (ZipEntry zipEntry in zipFile)
                                {
                                    extractProgress?.TickIncrement(increment);
                                    if (!zipEntry.IsFile)
                                    {
                                        continue;   // Ignore directories
                                    }

                                    byte[] buffer = new byte[Constants.StreamCopyBufferSize]; // 4K is optimum

                                    using (Stream zipStream = zipFile.GetInputStream(zipEntry))
                                    {
                                        string[]         path            = fileSystem.GetPath(zipEntry.Name);
                                        VirtualDirectory fileDestination = destination.Directory(path.Take(path.Length - 1).ToArray());
                                        VirtualFile      entryFile       = fileDestination.File(path.Last());

                                        // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
                                        // of the file, but does not waste memory.
                                        using (Stream streamWriter = entryFile.OpenWrite())
                                        {
                                            StreamUtils.Copy(zipStream, streamWriter, buffer);
                                        }
                                    }
                                }
                            }
                        }
            }

            VirtualFile UnpackXzFile(VirtualFile packedFile)
            {
                using (Stream fileStream = packedFile.OpenRead())
                    using (XZStream xzStream = new XZStream(fileStream))
                        using (parentProgress?.SpawnInfiniteProgress($"Extracting .xz file {packedFile.Name}..."))
                        {
                            string[] path             = fileSystem.GetPath(packedFile.FullName);
                            string   relativeFilePath = path.Last().Substring(0, path.Last().LastIndexOf(".xz", StringComparison.OrdinalIgnoreCase));
                            if (destination.FileExists(relativeFilePath))
                            {
                                destination.File(relativeFilePath).Delete();
                            }
                            VirtualFile destinationFile = destination.File(relativeFilePath);
                            observable?.OnNext(new Change(() => destinationFile.Restore()));

                            byte[] buffer = new byte[Constants.StreamCopyBufferSize]; // 4K is optimum

                            using (Stream streamWriter = destinationFile.OpenWrite())
                            {
                                StreamUtils.Copy(xzStream, streamWriter, buffer);
                                return(destinationFile);
                            }
                        }
            }

            void UnpackTarFile(VirtualFile packedFile)
            {
                //sharpcompress
                using (Stream fileStream = packedFile.OpenRead())
                    using (TarArchive tarArchive = TarArchive.Open(fileStream))
                    {
                        double entriesUntilTickIncrement = tarArchive.Entries.Count / (double)Constants.ProgressUpdateInterval;
                        using (IProgressNotifier extractProgress = parentProgress?.Spawn(Constants.ProgressMaxResolution, "Extracting .tar archive"))
                        {
                            int entriesSinceLastTickIncrement = 0;
                            foreach (TarArchiveEntry tarEntry in tarArchive.Entries)
                            {
                                if (entriesSinceLastTickIncrement > entriesUntilTickIncrement)
                                {
                                    extractProgress?.TickIncrement(Constants.ProgressUpdateInterval);
                                    entriesSinceLastTickIncrement = 0;
                                }

                                if (tarEntry.IsDirectory)
                                {
                                    continue;       // Ignore directories
                                }

                                byte[] buffer = new byte[Constants.StreamCopyBufferSize]; // 4K is optimum

                                using (Stream tarStream = tarEntry.OpenEntryStream())
                                {
                                    string[]         path            = fileSystem.GetPath(tarEntry.Key);
                                    VirtualDirectory fileDestination = destination.Directory(path.Take(path.Length - 1).ToArray());
                                    if (fileDestination.FileExists(path.Last()))
                                    {
                                        fileDestination.File(path.Last()).Delete();
                                    }
                                    VirtualFile entryFile = fileDestination.File(path.Last());
                                    observable?.OnNext(new Change(() => entryFile.Restore()
                                                                  //, $"Unpack {tarEntry.Key}."
                                                                  ));

                                    //Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
                                    // of the file, but does not waste memory.
                                    using (Stream streamWriter = entryFile.OpenWrite())
                                    {
                                        StreamUtils.Copy(tarStream, streamWriter, buffer);
                                    }
                                }

                                entriesSinceLastTickIncrement++;
                            }
                            extractProgress?.Tick(Constants.ProgressMaxResolution);
                        }
                    }
            }

            void UnpackTarXzFile(VirtualFile packedFile)
            {
                using (IProgressNotifier subProgress = parentProgress?.Spawn(2, "Extracting tar.xz file"))
                {
                    parentProgress = subProgress;
                    VirtualFile tarFile = UnpackXzFile(packedFile);
                    UnpackTarFile(tarFile);
                    tarFile.Delete();
                }
            }
        }
示例#15
0
 public IndexedArchive(VirtualFile file)
 {
     File = file;
 }
示例#16
0
 public RawSourceFile(VirtualFile file, RelativePath path)
 {
     File = file;
     Path = path;
 }
示例#17
0
 internal MutableProjectSettings(ProjectSettings value, VirtualFile projectFile, ExecutionContext executionContext)
 {
     this.projectFile      = projectFile;
     this.executionContext = executionContext;
     Value = value;
 }
示例#18
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            //NeoAxis initialization
            EngineApp.ConfigName = "user:Configs/DedicatedServer.config";
            EngineApp.ReplaceRenderingSystemComponentName = "RenderingSystem_NULL";
            EngineApp.ReplaceSoundSystemComponentName     = "SoundSystem_NULL";
            if (!WinFormsAppWorld.Init(new WinFormsAppEngineApp(EngineApp.ApplicationTypes.Simulation), this,
                                       "user:Logs/DedicatedServer.log", true, null, null, null, null))
            {
                Close();
                return;
            }
            WinFormsAppEngineApp.Instance.AutomaticTicks = false;

            Engine.Log.Handlers.InfoHandler += delegate(string text, ref bool dumpToLogFile)
            {
                Log("Log: " + text);
            };

            Engine.Log.Handlers.ErrorHandler += delegate(string text, ref bool handled, ref bool dumpToLogFile)
            {
                handled = true;
                timer1.Stop();
                MessageBox.Show(text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                timer1.Start();
            };

            Engine.Log.Handlers.FatalHandler += delegate(string text, string createdLogFilePath,
                                                         ref bool handled)
            {
                handled = true;
                timer1.Stop();
                MessageBox.Show(text, "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            };

            //register config fields of this class
            EngineApp.Instance.Config.RegisterClassParameters(GetType());

            //generate map list
            {
                string[] mapList = VirtualDirectory.GetFiles("", "*.map", SearchOption.AllDirectories);
                foreach (string mapName in mapList)
                {
                    //check for network support
                    if (VirtualFile.Exists(string.Format("{0}\\NoNetworkSupport.txt",
                                                         Path.GetDirectoryName(mapName))))
                    {
                        continue;
                    }

                    comboBoxMaps.Items.Add(mapName);
                    if (mapName == lastMapName)
                    {
                        comboBoxMaps.SelectedIndex = comboBoxMaps.Items.Count - 1;
                    }
                }

                comboBoxMaps.SelectedIndexChanged += comboBoxMaps_SelectedIndexChanged;
            }

            checkBoxLoadMapAtStartup.Checked          = loadMapAtStartup;
            checkBoxAllowCustomClientCommands.Checked = allowCustomClientCommands;

            //load map at startup
            if (loadMapAtStartup && comboBoxMaps.SelectedItem != null)
            {
                Create();
                string mapName = comboBoxMaps.SelectedItem as string;
                if (!MapLoad(mapName))
                {
                    return;
                }
            }
        }
示例#19
0
        /// <inheritdoc />
        public VirtualFileSystemEntry Build(VirtualDirectory root, string hostOutputPath = null, string gameName = null, bool useCompression = false, bool useExtracted = false)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }

            Log.Builder.Info($"Building {Game} Mod");
            if (hostOutputPath != null)
            {
                Log.Builder.Info($"Output directory: {hostOutputPath}");
            }

            // Get game config
            var config = ConfigStore.Get(Game) as Persona34GameConfig ?? throw new InvalidOperationException("Game config is missing.");

            if (string.IsNullOrWhiteSpace(config.DvdRootOrIsoPath))
            {
                throw new InvalidConfigException("Dvd root path/ISO path is not specified.");
            }

            // Get files
            Log.Builder.Trace($"DvdRootOrIsoPath = {config.DvdRootOrIsoPath}");
            var dvdRootDirectory = Persona34Common.GetRootDirectory(config, out var isoFileSystem);

            // Find system config
            var systemConfigFile = dvdRootDirectory["SYSTEM.CNF"] as VirtualFile ?? throw new MissingFileException("SYSTEM.CNF is missing from the file source.");

            string executablePath;

            using (var systemConfigStream = systemConfigFile.Open())
            {
                bool leaveOpen = isoFileSystem == null;
                executablePath = Ps2SystemConfig.GetExecutablePath(systemConfigStream, leaveOpen, true);
            }

            if (executablePath == null)
            {
                throw new MissingFileException("Executable file path is not specified in SYSTEM.CNF; Unable to locate executable file.");
            }

            Log.Builder.Info($"Executable path: {executablePath}");

            var executableFile = ( VirtualFile )dvdRootDirectory[executablePath] ?? throw new MissingFileException("The executable file is missing from the dvd root file source.");

            // Some basic checks have been done, let's start generating the cvms
            var dvdRootDirectoryPath = hostOutputPath ?? Path.Combine(Path.GetTempPath(), "Persona34ModCompilerTemp_" + Path.GetRandomFileName());

            Log.Builder.Trace($"Creating (temp?) output directory: {dvdRootDirectoryPath}");
            Directory.CreateDirectory(dvdRootDirectoryPath);

            var bgmCvmFile      = ( VirtualFile )dvdRootDirectory["BGM.CVM"];
            var bgmCvmModified  = false;
            var btlCvmFile      = ( VirtualFile )dvdRootDirectory["BTL.CVM"];
            var btlCvmModified  = false;
            var dataCvmFile     = ( VirtualFile )dvdRootDirectory["DATA.CVM"];
            var dataCvmModified = false;
            var envCvmFile      = ( VirtualFile )dvdRootDirectory["ENV.CVM"];
            var envCvmModified  = false;

            var newDvdRootDirectory = new VirtualDirectory();

            // Process mod files
            Log.Builder.Info("Processing mod files");
            foreach (var entry in root)
            {
                if (entry.EntryType == VirtualFileSystemEntryType.File)
                {
                    Log.Builder.Info($"Adding file {entry.Name} to root directory");
                    entry.MoveTo(newDvdRootDirectory, true);
                    continue;
                }

                var name      = entry.Name.ToLowerInvariant();
                var directory = ( VirtualDirectory )entry;

                switch (name)
                {
                case "bgm":
                    UpdateAndRecompileCvm(ref bgmCvmFile, directory, Path.Combine(dvdRootDirectoryPath, "bgm.cvm"), newDvdRootDirectory);
                    bgmCvmModified = true;
                    break;

                case "btl":
                    UpdateAndRecompileCvm(ref btlCvmFile, directory, Path.Combine(dvdRootDirectoryPath, "btl.cvm"), newDvdRootDirectory);
                    btlCvmModified = true;
                    break;

                case "data":
                    UpdateAndRecompileCvm(ref dataCvmFile, directory, Path.Combine(dvdRootDirectoryPath, "data.cvm"), newDvdRootDirectory);
                    dataCvmModified = true;
                    break;

                case "env":
                {
                    Log.Builder.Info("Replacing files in env.cvm");

                    if (envCvmFile == null)
                    {
                        throw new MissingFileException("Mod replaces files in env.cvm but env.cvm isn't present.");
                    }

                    UpdateAndRecompileCvm(ref envCvmFile, directory, Path.Combine(dvdRootDirectoryPath, "env.cvm"), newDvdRootDirectory);
                    envCvmModified = true;
                }
                break;

                default:
                    Log.Builder.Info($"Adding directory {entry.Name} to root directory");
                    entry.MoveTo(newDvdRootDirectory, true);
                    break;
                }
            }

            // Patch executable
            var executableFilePath = executableFile.SaveToHost(dvdRootDirectoryPath);

            Log.Builder.Info($"Patching executable");
            Log.Builder.Trace($"Executable file path: {executableFilePath}");

            if (bgmCvmModified)
            {
                PatchExecutable(executableFilePath, bgmCvmFile.HostPath);
            }

            if (btlCvmModified)
            {
                PatchExecutable(executableFilePath, btlCvmFile.HostPath);
            }

            if (dataCvmModified)
            {
                PatchExecutable(executableFilePath, dataCvmFile.HostPath);
            }

            if (envCvmModified)
            {
                PatchExecutable(executableFilePath, envCvmFile.HostPath);
            }

            executableFile = VirtualFile.FromHostFile(executableFilePath);
            executableFile.MoveTo(newDvdRootDirectory, true);

            if (hostOutputPath != null)
            {
                Log.Builder.Info($"Copying files to output directory (might take a while): {hostOutputPath}");
                newDvdRootDirectory.SaveToHost(hostOutputPath);
            }

            if (hostOutputPath != null && isoFileSystem != null)
            {
                isoFileSystem.Dispose();
            }

            Log.Builder.Info("Done");

            return(newDvdRootDirectory);
        }
        internal static void ProcessRequest(HttpContextBase context, VirtualFileReader fileReader = null, Action <string, Exception> logAction = null, bool validatePath = true)
        {
            string decryptedString           = null;
            bool   resourceIdentifierPresent = false;

            try {
                HttpResponseBase response = context.Response;
                response.Clear();

                if (validatePath)
                {
                    // Checking that the handler is not being called from a different path.
                    EnsureScriptResourceRequest(context.Request.Path);
                }

                string encryptedData = context.Request.QueryString["d"];
                if (String.IsNullOrEmpty(encryptedData))
                {
                    Throw404();
                }

                resourceIdentifierPresent = true;
                try {
                    decryptedString = Page.DecryptString(encryptedData, Purpose.ScriptResourceHandler_ScriptResourceUrl);
                }
                catch (CryptographicException ex) {
                    Throw404(ex);
                }

                fileReader = fileReader ?? new VirtualFileReader(delegate(string virtualPath, out Encoding encoding) {
                    VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
                    if (!vpp.FileExists(virtualPath))
                    {
                        Throw404();
                    }
                    VirtualFile file = vpp.GetFile(virtualPath);
                    if (!AppSettings.ScriptResourceAllowNonJsFiles && !file.Name.EndsWith(".js", StringComparison.OrdinalIgnoreCase))
                    {
                        // MSRC 10405: Disallow all extensions other than *.js
                        Throw404();
                    }
                    using (Stream stream = file.Open()) {
                        using (StreamReader reader = new StreamReader(stream, true)) {
                            encoding = reader.CurrentEncoding;
                            return(reader.ReadToEnd());
                        }
                    }
                });
                ProcessRequestInternal(response, decryptedString, fileReader);
            } catch (Exception e) {
                if (resourceIdentifierPresent)
                {
                    logAction = logAction ?? AssemblyResourceLoader.LogWebResourceFailure;
                    logAction(decryptedString, e);
                }
                // MSRC 10405: There's no reason for this to return anything other than a 404 if something
                // goes wrong. We shouldn't propagate the inner exception inside the YSOD, as it might
                // contain sensitive cryptographic information.
                Throw404();
            }
        }
示例#21
0
 protected static string ReadToEnd(VirtualFile file)
 {
     using (var fs = file.Open())
     {
         var reader = new StreamReader(fs, Encoding.UTF8, true, (int) VirtualFileSystem.DefaultBlockSize, true);
         return reader.ReadToEnd();
     }
 }
示例#22
0
        public override VirtualFile GetFile(string virtualPath)
        {
            VirtualFile vf = base.GetFile(virtualPath);

            return(vf);
        }