Skip to content

Commit 7552af1

Browse files
committed
add moddependency
1 parent 86c729f commit 7552af1

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

polymod/Polymod.hx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -817,12 +817,17 @@ typedef ModContributor =
817817
};
818818

819819
/**
820-
* A type representing a mod's dependencies.
821-
*
822-
* The map takes the mod's ID as the key and the required version as the value.
823-
* The version follows the Semantic Versioning format, with `*.*.*` meaning any version.
820+
* A type representing a mod's dependency.
821+
* The `version` field is the version of the dependency
822+
* The `url` field is the Github Repository's url to download the dependency from
823+
* The `commit` field is the commit hash of the dependency
824824
*/
825-
typedef ModDependencies = Map<String, VersionRule>;
825+
typedef ModDependency =
826+
{
827+
version:VersionRule,
828+
?url:String,
829+
?commit:String
830+
};
826831

827832
/**
828833
* A type representing data about a mod, as retrieved from its metadata file.
@@ -894,14 +899,14 @@ class ModMetadata
894899
* These other mods must be also be loaded in order for this mod to load,
895900
* and this mod must be loaded after the dependencies.
896901
*/
897-
public var dependencies:ModDependencies;
902+
public var dependencies:Map<String, ModDependency>;
898903

899904
/**
900905
* A list of dependencies.
901906
* This mod must be loaded after the optional dependencies,
902907
* but those mods do not necessarily need to be loaded.
903908
*/
904-
public var optionalDependencies:ModDependencies;
909+
public var optionalDependencies:Map<String, ModDependency>;
905910

906911
/**
907912
* A deprecated field representing the mod's author.
@@ -1014,8 +1019,10 @@ class ModMetadata
10141019
m.license = JsonHelp.str(json, 'license');
10151020
m.metadata = JsonHelp.mapStr(json, 'metadata');
10161021

1017-
m.dependencies = JsonHelp.mapVersionRule(json, 'dependencies');
1018-
m.optionalDependencies = JsonHelp.mapVersionRule(json, 'optionalDependencies');
1022+
m.dependencies = JsonHelp.mapModDependency(json, 'dependencies');
1023+
m.optionalDependencies = JsonHelp.mapModDependency(json, 'optionalDependencies');
1024+
1025+
trace(m.dependencies);
10191026

10201027
return m;
10211028
}

polymod/format/JsonHelp.hx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package polymod.format;
22

3+
import polymod.Polymod.ModDependency;
34
import thx.semver.VersionRule;
45

56
class JsonHelp
@@ -89,6 +90,25 @@ class JsonHelp
8990
return map;
9091
}
9192

93+
public static function mapModDependency(json:Dynamic, field:String):Map<String, ModDependency>
94+
{
95+
var map:Map<String, ModDependency> = new Map<String, ModDependency>();
96+
if (json == null || field == '' || field == null)
97+
return map;
98+
var val = null;
99+
if (Reflect.hasField(json, field))
100+
val = Reflect.field(json, field);
101+
if (val != null)
102+
{
103+
for (field in Reflect.fields(val))
104+
{
105+
var fieldVal = Reflect.field(val, field);
106+
map.set(field, fieldVal);
107+
}
108+
}
109+
return map;
110+
}
111+
92112
public static function str(json:Dynamic, field:String, defaultValue:String = ''):String
93113
{
94114
var str:String = '';

polymod/util/DependencyUtil.hx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package polymod.util;
33
import haxe.Http;
44
import haxe.io.Bytes;
55
import haxe.zip.Reader;
6-
import polymod.Polymod.ModDependencies;
6+
import polymod.Polymod.ModDependency;
77
import polymod.Polymod.ModMetadata;
88
import sys.FileSystem;
99
import sys.io.File;
@@ -58,7 +58,7 @@ class DependencyUtil
5858
var result:Array<ModMetadata> = [];
5959

6060
// Compile a map of mod dependencies.
61-
var deps:ModDependencies = compileDependencies(modList);
61+
var deps:Map<String, ModDependency> = compileDependencies(modList);
6262

6363
// Check that all mods are in the mod list.
6464
var relevantMods:Array<ModMetadata> = [];
@@ -73,7 +73,7 @@ class DependencyUtil
7373
// Check that all dependencies are satisfied.
7474
for (dep in deps.keys())
7575
{
76-
var depRule:VersionRule = deps.get(dep);
76+
var depRule:VersionRule = deps.get(dep).version;
7777

7878
// Check that the dependency is in the mod list.
7979
var depMod:ModMetadata = null;
@@ -117,7 +117,7 @@ class DependencyUtil
117117
static function validateDependencies(modList:Array<ModMetadata>):Bool
118118
{
119119
// Compile a map of mod dependencies.
120-
var deps:ModDependencies = compileDependencies(modList);
120+
var deps:Map<String, ModDependency> = compileDependencies(modList);
121121

122122
// Check that all mods are in the mod list.
123123
var relevantMods:Array<ModMetadata> = [];
@@ -132,7 +132,7 @@ class DependencyUtil
132132
// Check that all dependencies are satisfied.
133133
for (dep in deps.keys())
134134
{
135-
var depRule:VersionRule = deps.get(dep);
135+
var depRule:VersionRule = deps.get(dep).version;
136136

137137
// Check that the dependency is in the mod list.
138138
var depMod:ModMetadata = null;
@@ -312,28 +312,37 @@ class DependencyUtil
312312
* For example, if one mod requires `>1.2.0` of `modA` and another requires `>1.3.0` of `modA`,
313313
* the merged list will be `[modA: '>1.2.0 && >1.3.0']`.
314314
*/
315-
public static function compileDependencies(modList:Array<ModMetadata>):Map<String, VersionRule>
315+
public static function compileDependencies(modList:Array<ModMetadata>):Map<String, ModDependency>
316316
{
317-
var result:Map<String, VersionRule> = [];
317+
var result:Map<String, ModDependency> = [];
318318

319319
for (mod in modList)
320320
{
321321
if (result[mod.id] == null)
322-
result[mod.id] = VersionUtil.DEFAULT_VERSION_RULE;
322+
result[mod.id] = {
323+
version: VersionUtil.DEFAULT_VERSION_RULE
324+
};
323325

324326
if (mod.dependencies != null)
325327
{
326328
for (dependencyId in mod.dependencies.keys())
327329
{
328-
var dependencyRule:VersionRule = mod.dependencies[dependencyId];
330+
if (result[dependencyId] == null)
331+
result[dependencyId] = {
332+
version: VersionUtil.DEFAULT_VERSION_RULE
333+
};
329334

330-
if (result[dependencyId] != null)
335+
result[dependencyId].url = mod.dependencies[dependencyId].url;
336+
result[dependencyId].commit = mod.dependencies[dependencyId].commit;
337+
var dependencyRule:VersionRule = mod.dependencies[dependencyId].version;
338+
339+
if (result[dependencyId].version != null)
331340
{
332-
result[dependencyId] = VersionUtil.combineRulesAnd(result[dependencyId], dependencyRule);
341+
result[dependencyId].version = VersionUtil.combineRulesAnd(result[dependencyId].version, dependencyRule);
333342
}
334343
else
335344
{
336-
result[dependencyId] = dependencyRule;
345+
result[dependencyId].version = dependencyRule;
337346
}
338347
}
339348
}

0 commit comments

Comments
 (0)