This repository was archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
98 lines (86 loc) · 2.69 KB
/
server.rb
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
require_relative "plugin"
require "json"
# Represents a local Minecraft server in a Docker container.
class LocalServer
def initialize(path=nil, data={})
@path = File.expand_path(path || "~")
@data = data
end
# Get the absolute path that the server operates inside.
def path
@path
end
# Get the data passed in for the server.
def data
@data
end
# Path of extra config directories to load from
def load_path
data[:load_path].nil? ? "" : data[:load_path]
end
# Path of plugin configuration files
def plugins_path
data[:plugins_path]
end
# Array of {spurce: '', destination: ''} hashes demoting files in remote directories
def remote_files
data[:remote_files].nil? ? [] : data[:remote_files]
end
# Array of {name: '', jar_path: ''} hashes denoting plugins to be loaded
def plugins
data[:plugins].nil? ? [] : data[:plugins].map { |pl| JSON.parse(pl) }
end
# Games external components to load in an array of {path: '', name: ''} hashes
def components
data[:components].nil? ? [] : data[:components].map { |comp| JSON.parse(comp) }
end
# Environment variables
def env
data[:env].nil? ? {} : data[:env]
end
def describe
"Load Path: #{load_path}\n" +
"Plugins Path: #{plugins_path}\n" +
"Remote Files: #{remote_files}\n" +
"Plugins: #{plugins}\n" +
"Components: #{components}\n" +
"Environment: #{env}"
end
# Move over files from the data folder, format plugin configuration files,
# ensure at least one map available, and inject server variables into text-based files.
def load!
plug_path = "#{path}/plugins"
unless File.directory?(plug_path)
FileUtils.mkdir_p(plug_path)
end
for folder in ["base", load_path]
FileUtils.copy_entry("/data/servers/#{folder}", "#{path}")
end
remote_files.each do |file|
FileUtils.copy_entry(file[:source], "#{path}/#{file[:destination]}")
end
plugins.each do |plugin|
Plugin.new(plugin[:name]).load_and_save!(
"/data/plugins/#{plugins_path}",
plugin[:jar_path],
"#{path}/plugins"
)
end
unless components.empty?
comp_path = "#{path}/plugins/GameManager/components/"
unless File.directory?(comp_path)
FileUtils.mkdir_p(comp_path)
end
components.each do |component|
FileUtils.copy_entry(component[:path], "#{path}/plugins/GameManager/components/#{component[:name]}.jar")
end
end
env.each{|k,v| Env.set(k, v.to_s, true)}
for file in ["yml", "yaml", "json", "properties"].flat_map{|ext| Dir.glob("#{path}/**/*.#{ext}")}
data = Env.substitute(File.read(file))
File.open(file, "w") do |f|
f.write(data)
end
end
end
end