-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectsFileItemTypeLoader.cs
198 lines (166 loc) · 7.36 KB
/
ObjectsFileItemTypeLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// -----------------------------------------------------------------
// <copyright file="ObjectsFileItemTypeLoader.cs" company="2Dudes">
// Copyright (c) | Jose L. Nunez de Caceres et al.
// https://linkedin.com/in/nunezdecaceres
//
// All Rights Reserved.
//
// Licensed under the MIT License. See LICENSE in the project root for license information.
// </copyright>
// -----------------------------------------------------------------
namespace Fibula.Plugins.ItemLoaders.CipObjectsFile
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Fibula.Data.Contracts.Abstractions;
using Fibula.Definitions.Data.Entities;
using Fibula.Definitions.Enumerations;
using Fibula.Definitions.Flags;
using Fibula.Parsing.CipFiles;
using Fibula.Parsing.CipFiles.Enumerations;
using Fibula.Parsing.CipFiles.Extensions;
using Fibula.Utilities.Validation;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
/// <summary>
/// Class that represents an item type loader that reads from the objects file.
/// </summary>
/// <remarks>
///
/// An item definition starts and ends with blank lines.
///
/// TypeID = 1 # body container
/// Name = ""
/// Flags = {Container,Take}
/// Attributes = {Capacity=1,Weight=0}
/// .
/// </remarks>
public sealed class ObjectsFileItemTypeLoader : IItemTypesLoader
{
/// <summary>
/// Character for comments.
/// </summary>
public const char CommentSymbol = '#';
/// <summary>
/// Separator used for property and value pairs.
/// </summary>
public const char PropertyValueSeparator = '=';
/// <summary>
/// Initializes a new instance of the <see cref="ObjectsFileItemTypeLoader"/> class.
/// </summary>
/// <param name="logger">A reference to the logger instance.</param>
/// <param name="options">The options for this loader.</param>
public ObjectsFileItemTypeLoader(
ILogger<ObjectsFileItemTypeLoader> logger,
IOptions<ObjectsFileItemTypeLoaderOptions> options)
{
logger.ThrowIfNull(nameof(logger));
options.ThrowIfNull(nameof(options));
DataAnnotationsValidator.ValidateObjectRecursive(options.Value);
this.LoaderOptions = options.Value;
this.Logger = logger;
}
/// <summary>
/// Gets the loader options.
/// </summary>
public ObjectsFileItemTypeLoaderOptions LoaderOptions { get; }
/// <summary>
/// Gets the logger to use in this handler.
/// </summary>
public ILogger Logger { get; }
/// <summary>
/// Attempts to load the item catalog.
/// </summary>
/// <returns>The catalog, containing a mapping of loaded id to the item types.</returns>
public IDictionary<string, ItemTypeEntity> LoadTypes()
{
var itemDictionary = new Dictionary<string, ItemTypeEntity>();
var objectsFilePath = Path.Combine(Environment.CurrentDirectory, this.LoaderOptions.FilePath);
var currentType = new ItemTypeEntity();
foreach (var readLine in File.ReadLines(objectsFilePath))
{
if (readLine == null)
{
continue;
}
var inLine = readLine.Split(new[] { CommentSymbol }, 2).FirstOrDefault();
// ignore comments and empty lines.
if (string.IsNullOrWhiteSpace(inLine))
{
// wrap up the current ItemType and add it if it has enough properties set:
if (currentType.TypeId == 0 || string.IsNullOrWhiteSpace(currentType.Name))
{
continue;
}
itemDictionary.Add(currentType.TypeId.ToString(), currentType);
currentType = new ItemTypeEntity();
continue;
}
var data = inLine.Split(new[] { PropertyValueSeparator }, 2);
if (data.Length != 2)
{
throw new InvalidDataException($"Malformed line [{inLine}] in objects file: [{objectsFilePath}]");
}
var propName = data[0].ToLower().Trim();
var propData = data[1].Trim();
switch (propName)
{
case "typeid":
currentType.TypeId = Convert.ToUInt16(propData);
break;
case "name":
currentType.Name = propData.Substring(Math.Min(1, propData.Length), Math.Max(0, propData.Length - 2));
break;
case "description":
currentType.Description = propData;
break;
case "flags":
foreach (var element in CipFileParser.Parse(propData))
{
var flagName = element.Attributes.First().Name;
if (Enum.TryParse(flagName, out CipItemFlag flagMatch))
{
if (flagMatch.ToItemFlag() is ItemFlag itemflag)
{
currentType.SetItemFlag(itemflag);
}
continue;
}
this.Logger.LogWarning($"Unknown flag [{flagName}] found on item with TypeID [{currentType.TypeId}].");
}
break;
case "attributes":
foreach (var attrStr in propData.Substring(Math.Min(1, propData.Length), Math.Max(0, propData.Length - 2)).Split(','))
{
var attrPair = attrStr.Split('=');
if (attrPair.Length != 2)
{
this.Logger.LogError($"Invalid attribute {attrStr}.");
continue;
}
var attributeName = attrPair[0];
var attributeValue = Convert.ToInt32(attrPair[1]);
if (Enum.TryParse(attributeName, out CipItemAttribute cipAttribute))
{
if (cipAttribute.ToItemAttribute() is ItemAttribute itemAttribute)
{
currentType.SetAttribute(itemAttribute, attributeValue);
}
continue;
}
this.Logger.LogWarning($"Attempted to set an unknown item attribute [{attributeName}].");
}
break;
}
}
// wrap up the last ItemType and add it if it has enough properties set:
if (currentType.TypeId != 0 && !string.IsNullOrWhiteSpace(currentType.Name))
{
itemDictionary.Add(currentType.TypeId.ToString(), currentType);
}
return itemDictionary;
}
}
}