Example #1
0
 public static void AddError(Mod mod, Resource resource, string format, params object[] args)
 {
     if (mod == null)
         GlobalErrors.Add(new Error(null, string.Format(format, args)));
     else
         mod.Errors.Add(new Error(resource, string.Format(format, args)));
 }
Example #2
0
        private static bool CanOpen(Resource resource)
        {
            if (resource == null)
                return true;

            return resource.Mod.FileSystem.IsAccessible && resource.Exists;
        }
Example #3
0
 public ResourceViewModel(Resource resource, ICommand openJsonWindowCommand)
 {
     this.Resource = resource;
     this.References = resource.References.Select(m => new Views.ReferenceListItem(m, Views.ReferenceListItem.Display.NoMod, resource)).ToList();
     this.ReferredBy = resource.ReferredBy.Select(m => new Views.ReferenceListItem(m, Views.ReferenceListItem.Display.SourcedNoMod, resource)).ToList();
     this.OpenJsonWindowCommand = openJsonWindowCommand;
 }
Example #4
0
 public Error(Resource resource, string description)
 {
     this.Resource = resource;
     this.Description = description;
 }
Example #5
0
 public Mixinto(Resource definition, Resource origin, Resource target, string prefix)
     : base(definition, origin, target, "mixinto")
 {
 }
Example #6
0
        private static Reference resolve(Resource context, string value, bool createIfNonExistant)
        {
            if (IsIdentifier(value))
            {
                // Get the folder we are located in (to get some context)
                Pather p = new Pather(context.Location);
                p.Pop();

                if (Exists(value, p.ToString()) || IsFileReference(value))
                {
                    // The resource definitely exists.
                    Resource res;
                    TryGet(value, p.ToString(), out res);

                    Reference reference = context.References.FirstOrDefault(r => r.Target == res);

                    // Create the reference if it didn't already exist
                    if (reference == null && createIfNonExistant)
                    {
                        // Attempt to create an alias reference
                        reference = ParseAlias(value);
                        if (reference != null)
                            context.AddReference(reference);
                        else
                            reference = new Reference(context, context, res);
                    }
                    return reference;
                }
                else
                {
                    // It IS an alias of a mod we *know*?
                    int idx = value.IndexOf(':');
                    if (idx > 0 && idx < value.Length)
                    {
                        string modName = value.Substring(0, idx);
                        Mod mod;
                        if (mods.TryGetValue(modName, out mod))
                        {
                            // The mod exists.
                            Alias alias = mod.Aliases.FirstOrDefault(a => a.FullName.Equals(value));

                            if (alias == null && createIfNonExistant && value.Length > idx + 1)
                            {
                                // Last chance: We're a virtual resource.
                                if (VirtualResources.Contains(value))
                                    alias = new Alias(mod, value.Substring(idx + 1), mod.VirtualResource);
                                else // Nope, missing.
                                    alias = new Alias(mod, value.Substring(idx + 1), mod.InvalidResource);
                            }

                            return alias;
                        }
                    }
                    // Is it a file identifier?
                    else if (IsFileReference(value))
                    {
                        // Well, I guess it's a broken one.
                        Resource res;
                        TryGet(value, p.ToString(), out res);

                        Reference reference = context.References.FirstOrDefault(r => r.Target == res);

                        // Create the reference if it didn't already exist
                        if (reference == null && createIfNonExistant)
                            reference = new Reference(context, context, res);

                        return reference;
                    }
                }
            }

            return null;
        }
Example #7
0
        private static void addToContext(Resource resource, JToken token)
        {
            if (token.Type != JTokenType.String)
                return;

            string value = token.ToString();

            Reference reference = resolve(resource, value, true);
        }
Example #8
0
        /// <summary>
        /// Tries to fetch said entry. If it does not exist, creates it.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryGet(string name, string context, out Resource result)
        {
            Mod mod;
            Pather p;

            // Is it an alias?
            Alias al = ParseAlias(name);
            if (al != null)
            {
                result = al.Target;
                return true;
            }

            // If we can't construct a pather then all is lost
            if (!ConstructPather(name, context, out mod, out p))
            {
                result = null;
                return false;
            }

            // The... uh... uhhhhm....
            // The... resolved path. Yes. That sounds good enough.
            var fullPath = p.ToString();

            // Try to fetch the resource from our list?
            if (Resources.TryGetValue(fullPath, out result))
                return true;

            // Checks if the path exists + adjusts our pather
            bool exists = Exists(mod.FileSystem, p, false);

            fullPath = p.ToString();

            // Try again with our new path?
            if (Resources.TryGetValue(fullPath, out result))
                return true;

            // Create a new resource then.
            string location = p.ToString();
            if (Path.GetExtension(location) == ".json")
                result = new JsonResource(location, mod, exists);
            else
                result = new Resource(location, mod, exists);
            Resources.Add(location, result);

            if (exists && result is JsonResource)
                Queue.Enqueue(result);

            return exists;
        }
Example #9
0
 public static Stream OpenRead(Resource resource)
 {
     return resource.Mod.FileSystem.OpenRead(new Pather(resource.Location).RootlessPath);
 }
Example #10
0
        private void addReference(Resource keyRes, string value, Action<Resource, Resource> action)
        {
            Resource valueRes;

            if (!ResourceManager.TryGet(value, this.Name, out valueRes))
            {
                if (valueRes == null)
                {
                    ErrorReporter.AddError(this, this.Manifest, "Invalid reference value for {0}: unknown... stuff.", value);
                    return;
                }
                else
                    ErrorReporter.AddError(this, this.Manifest, "Missing resource for {0}", value);
            }

            action(keyRes, valueRes);
        }
Example #11
0
        /// <summary>
        /// Mixintos stuff.
        /// </summary>
        /// <param name="mixintoResource"></param>
        /// <param name="origin"></param>
        /// <param name="mixinto"></param>
        private static JToken mergeTokens(Resource targetResource, Resource mixintoResource, JToken origin, JToken mixinto)
        {
            // Compare types.
            Debug.Assert(origin != null);

            if (origin == null)
            {
                ErrorReporter.AddError(mixintoResource.Mod, mixintoResource, "Cannot mixinto {0} into {1}: Target has invalid JSON", mixintoResource.Location, targetResource.Location);
                return null;
            }

            if (mixinto == null)
            {
                ErrorReporter.AddError(targetResource.Mod, targetResource, "Cannot mixinto {0} into {1}: Mixinto has invalid JSON", mixintoResource.Location, targetResource.Location);
                return null;
            }

            JTokenType originType = origin.Type, mixintoType = mixinto.Type;

            if (originType == mixintoType)
            {
                switch (originType)
                {
                    case JTokenType.Object:
                        JObject originObj = (JObject)origin, mixintoObj = (JObject)mixinto;
                        foreach (var property in mixintoObj)
                        {
                            if (originObj[property.Key] == null)
                                originObj[property.Key] = property.Value;
                            else
                                originObj[property.Key] = mergeTokens(targetResource, mixintoResource, (JToken)originObj[property.Key], property.Value);
                        }

                        return originObj;
                    case JTokenType.Array:
                        JArray originArray = (JArray)origin, mixintoArray = (JArray)mixinto;
                        foreach (var item in mixinto.Reverse())
                            originArray.AddFirst(item);
                        return originArray;
                    case JTokenType.Integer:
                        return mixinto.Value<int>();
                    case JTokenType.String:
                        origin = mixinto.Value<string>();
                        return mixinto.Value<string>();
                    case JTokenType.Float:
                        return mixinto.Value<float>();
                    default:
                        ErrorReporter.AddError(mixintoResource.Mod, mixintoResource, "Invalid mixinto type {0} at {1}", mixinto.Type, mixinto.Path);
                        return null;
                }
            }
            else if (IncompatibleTypes(originType, mixintoType))
            {
                ErrorReporter.AddError(mixintoResource.Mod, mixintoResource, "Cannot mixinto {3}: Types for {2} differ (original: {0}; mixinto: {1})", originType, mixintoType, origin.Path, mixintoResource.Location);
                mixintoResource.Invalidate();
                return null;
            }

            return null;
        }
Example #12
0
 public override string ToStringSourced(bool addMod, Resource context)
 {
     return "[alias] " + FullName;
 }
Example #13
0
 public override string ToString(bool addMod, Resource resource)
 {
     return "[alias] " + FullName;
 }
Example #14
0
 public Alias(Mod mod, string name, Resource definition, Resource target)
     : base(definition, definition, target, "alias")
 {
     this.Name = name;
     mod.AddAlias(this);
 }
Example #15
0
 public Alias(Mod mod, string name, Resource target)
     : this(mod, name, mod.Manifest, target)
 {
 }
Example #16
0
        private bool validateType(JToken token, JTokenType type, Resource resource, string format)
        {
            if (token == null)
                return false;

            if (token.Type == type)
                return true;
            else
                ErrorReporter.AddError(this, resource, format, token.Type);

            return false;
        }
Example #17
0
        /// <summary>
        /// Loads a JSON resource.
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        public static JToken LoadJson(Resource resource)
        {
            JToken result = null;

            if (JsonCache.TryGetValue(resource, out result))
                return result;

            try
            {
                using (TextReader textReader = new StreamReader(OpenRead(resource)))
                using (JsonReader jsonReader = new JsonTextReader(textReader))
                    result = JObject.ReadFrom(jsonReader);
            }
            catch (JsonReaderException ex)
            {
                ErrorReporter.AddError(resource.Mod, resource, "Parsing json {0} failed: {1} at line {2}, position {3} (path {4})", resource.Location, ex.Message, ex.LineNumber, ex.LinePosition, ex.Path);
            }
            catch (JsonException ex)
            {
                ErrorReporter.AddError(resource.Mod, resource, "Parsing json {0} failed: {1}", resource.Location, ex.Message);
            }
            catch (IOException ex)
            {
                ErrorReporter.AddError(resource.Mod, resource, "Opening {0} to parse json failed: {1}", resource.Location, ex.Message);
            }

            JsonCache.Add(resource, result);

            return result;
        }
Example #18
0
        public void AddResource(Resource res)
        {
            if (res.Mod != this)
                throw new ArgumentException("resource must belong to this mod", "res");

            this.Resources.Add(res);
        }