This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Loader.cs
executable file
·163 lines (118 loc) · 4.4 KB
/
Loader.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
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
// portions of the code. See LICENSE file in the project root for full
// license information. Third-party libraries used by Pixel Vision 8 are
// under their own licenses. Please refer to those libraries for details
// on the license they use.
//
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//
// Jesse Freeman - @JesseFreeman
// Christina-Antoinette Neofotistou @CastPixel
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
//
using Microsoft.Xna.Framework.Graphics;
using PixelVision8.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace PixelVision8.Runner
{
public partial class Loader
{
public class FileParser : Attribute
{
public string FileType;
public MethodInfo MethodInfo;
public FileParser(string fileType)
{
FileType = fileType;
}
}
public int currentStep;
protected readonly List<IAbstractParser> parsers = new List<IAbstractParser>();
protected int currentParserID;
public bool Completed => currentParserID >= TotalParsers;
public float Percent => TotalSteps == 0 ? 1f : currentStep / (float)TotalSteps;
public int TotalParsers => parsers.Count;
public int TotalSteps;
private readonly IFileLoader _fileLoadHelper;
public List<FileParser> ParserMapping = new List<FileParser>();
private IImageParser _imageParser;
public Loader(IFileLoader fileLoadHelper, IImageParser imageParser)
{
_fileLoadHelper = fileLoadHelper;
_imageParser = imageParser;
var methods = GetType().GetMethods().Where(m=>m.GetCustomAttributes(typeof(FileParser), false).Length > 0).ToArray();
for (int i = 0; i < methods.Length; i++)
{
Console.WriteLine("Method " + i + " " + methods[i].Name);
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(methods[i].Name);
// Get the File Parser attribute
var attributes = theMethod.GetCustomAttribute(typeof(FileParser)) as FileParser;
// Cache the method info on the attribute instance
attributes.MethodInfo = theMethod;
ParserMapping.Add(attributes);
}
}
public void Reset()
{
parsers.Clear();
currentParserID = 0;
TotalSteps = 0;
currentStep = 0;
}
public void AddParser(IAbstractParser parser)
{
parser.CalculateSteps();
parsers.Add(parser);
TotalSteps += parser.totalSteps;
}
public void LoadAll()
{
while (Completed == false) NextParser();
parsers.Clear();
}
public void NextParser()
{
if (Completed)
{
parsers.Clear();
return;
}
var parser = parsers[currentParserID];
parser.NextStep();
currentStep++;
if (parser.completed)
{
parser.Dispose();
currentParserID++;
}
}
public virtual void ParseFiles(string[] files, IPlayerChips engine)
{
Reset();
Array.Sort(files);
for (int i = 0; i < ParserMapping.Count; i++)
{
var parserInfo = ParserMapping[i];
var filesToParse = files.Where(f => f.EndsWith(parserInfo.FileType)).ToArray();
if (filesToParse.Length > 0)
{
for (int j = 0; j < filesToParse.Length; j++)
{
parserInfo.MethodInfo.Invoke(this, new object[]{filesToParse[j], engine});
}
}
}
}
}
}