-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyapp.rb
executable file
·149 lines (139 loc) · 4 KB
/
myapp.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
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
#!/usr/bin/env ruby
require 'sinatra'
require 'sinatra/reloader'
require 'json'
require 'sinatra/json'
require 'slim'
require 'pp'
require 'zip'
configure :production do
enable :reloader
end
MinecraftHome="/Users/virus/Library/Application Support/minecraft"
profiles=JSON.parse(
File.open("#{MinecraftHome}/launcher_profiles.json").read
)
get '/' do
@profiles=profiles
slim :index
end
get '/profile' do
redirect to('/') unless params[:name]
if profiles["profiles"].key?(params[:name])
@profile=profiles["profiles"][params[:name]]
slim :profile
else
slim :error, locals: {error_mes: "Not found such profile: #{params[:name]}"}
end
end
get '/modlist.?:format?' do
if params[:format] != "json"
slim :modlist
else
send_data={}
if profiles["profiles"].key?(params[:name])
gen_infos = lambda do |dirname|
infos={}
modfiles=Dir.new(dirname).each.drop_while{ |i| i =~ /^\.{1,2}$/}
modfiles.each do |filename|
name=dirname+"/"+filename
if FileTest.directory?(name)
infos[filename+"/"]=gen_infos.call(name)
else
if name !~ /\.(jar|zip)$/
next
end
Zip::File.open(name) do |zipfile|
if zipfile.find_entry('mcmod.info')
modinfo_text=zipfile.read('mcmod.info').gsub(/(\r\n|\r|\n)/,"")
if(modinfo_text.encoding !=Encoding::UTF_8)
modinfo_text.force_encoding("Shift_JIS").encode!("UTF-8")
end
begin
infos[filename]=JSON.load(modinfo_text)
rescue => ex
next
end
else
infos[filename]=nil
end
end
end
end
return infos
end
@modinfos=[]
@profile=profiles["profiles"][params[:name]]
unless @profile.key?("gameDir") && FileTest.directory?("#{@profile["gameDir"]}/mods")
send_data["error"]="This profile doesn't have mod dir: #{params[:name]}<br>#{@profile["gameDir"]}"
end
moddir_name=(@profile["gameDir"] || MinecraftHome)+"/mods"
send_data=gen_infos.call(moddir_name)
else
send_data["error"]= "Not found such profile: #{params[:name]}"
end
json send_data
end
end
get '/options' do
if profiles["profiles"].key?(params[:name])
@profile=profiles["profiles"][params[:name]]
@gamedir=@profile["gameDir"] || MinecraftHome
@optionfile= @gamedir + "/options.txt"
@options={}
if File.exist?(@optionfile)
File.open(@optionfile,"r") do |file|
file.each_line do |line|
key,value = line.split(":")
@options[key]=value
end
end
slim :options
else
slim :error, locals: {error_mes: "File not found: #{@optionfile}" }
end
else
slim :error, locals: {error_mes: "Not found such profile: #{params[:name]}"}
end
end
get '/options_key.?:format?' do
send_data={}
if profiles["profiles"].key?(params[:name])
@profile=profiles["profiles"][params[:name]]
@gamedir=@profile["gameDir"] || MinecraftHome
@optionfile= @gamedir + "/options.txt"
@options={}
if File.exist?(@optionfile)
File.open(@optionfile,"r") do |file|
file.each_line do |line|
key,value = line.chomp.split(":")
if key=~/key/
keys=key.split('.')
data=send_data
last_data=nil
for var in keys do
if data[var] == nil
data[var]={}
end
last_data=data
data=data[var]
end
last_data[keys.last]=value
end
end
end
slim :options
else
send_data["error"]="File not found: #{@optionfile}"
end
else
send_data["error"]= "Not found such profile: #{params[:name]}"
end
if params[:format] == "json"
json send_data
elsif send_data["error"] == nil
slim :options_key
else
slim :error, locals: {error_mes: send_data["error"] }
end
end