Repository.java
3.3 KB
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
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.core.api.repository;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger.Verbosity;
/**
* A mod container repository defined by JSON modlists
*/
public class Repository
{
/**
* Default root which relative paths are relative to, usually the game
* directory
*/
private final File root;
/**
* Fallback repository root in case things go wrong. Usually the versionsed
* mods directory
*/
private final File defaultRepositoryRoot;
/**
* Resolvers in this repository
*/
private final Map<String, JsonResolver> resolvers = new TreeMap<String, JsonResolver>();
/**
* Global list of resolved artefacts
*/
private final List<Artefact> artefacts = new ArrayList<Artefact>();
public Repository(File root, File defaultRepositoryRoot)
{
this.root = root;
this.defaultRepositoryRoot = defaultRepositoryRoot;
}
/**
* Get the repository root, usually the game dir
*/
public File getRoot()
{
return this.root;
}
/**
* Get the default repository location, usually the versioned mods dir
*/
public File getDefaultRepositoryRoot()
{
return this.defaultRepositoryRoot;
}
Map<String, JsonResolver> getResolvers()
{
return this.resolvers;
}
JsonResolver getResolver(File path)
{
String location = path.getAbsolutePath();
JsonResolver resolver = this.resolvers.get(location);
if (resolver == null)
{
resolver = JsonResolver.createFrom(path);
this.resolvers.put(location, resolver);
}
return resolver;
}
/**
* Resolve the specified modlist
*
* @param path modlist path
*/
public void resolve(File path)
{
JsonResolver resolver = this.getResolver(path);
resolver.resolve(this);
this.artefacts.addAll(resolver.getArtefacts().values());
}
/**
* Get resolved artefacts from this repository
*/
public Collection<Artefact> getArtefacts()
{
return Collections.<Artefact>unmodifiableList(this.artefacts);
}
/**
* Get all resolved files in this repository
*/
public Collection<File> getFiles()
{
Builder<File> files = ImmutableList.<File>builder();
for (Artefact artefact : this.artefacts)
{
if (artefact.exists())
{
files.add(artefact.getFile());
}
else
{
LiteLoaderLogger.info(Verbosity.REDUCED, "Rejecting non-existent artefact %s at %s", artefact.getArtefactId(), artefact.getFile());
}
}
return files.build();
}
static boolean isAbsolutePath(String path)
{
return path.matches("^([a-zA-Z]:[/\\\\]|/).+$");
}
}