1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # GNU/Linux client with config saved to `config.json`
20 import json
::serialization
24 # Configuration of the client
29 var res_x
= 1920 is lazy
32 var res_y
= 1080 is lazy
34 # Should the client play sounds?
35 var play_sounds
= true is lazy
39 private var config_path
: String = sys
.program_name
.dirname
/ "../config.json"
41 private var config
: ClientConfig do
42 if config_path
.file_exists
then
43 var content
= config_path
.to_path
.read_all
44 var deser
= new JsonDeserializer(content
)
45 var cc
= deser
.deserialize
48 print_error
"Client Error: Deserializing config file failed with {deser.errors.join(", ")}"
49 else if not cc
isa ClientConfig then
50 print_error
"Client Error: Deserializing config file failed, got '{cc}'"
51 # TODO simplify the previous lines with ? or similar
55 # Save the default config to pretty Json
56 var cc
= new ClientConfig
57 var json
= cc
.to_plain_json
58 json
= json
.replace
(",", ",\n")
59 json
.write_to_file config_path
66 redef fun wanted_width
do return app
.config
.res_x
67 redef fun wanted_height
do return app
.config
.res_y
71 redef fun play
do if app
.config
.play_sounds
then super
74 redef class JsonDeserializer
75 # The only class we deserialize from pretty Json is ClientConfig
76 redef fun class_name_heuristic
(object
) do return "ClientConfig"