switched to iv.base64; added protection from invalid images
[xreader.git] / xreadercfg.d
blob46a523807879b64598c7ab4d8290144faf578f0e
1 /* Written by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module xreadercfg;
19 import arsd.simpledisplay;
20 import arsd.image;
22 import iv.nanovg;
23 import iv.strex;
24 import iv.vfs;
25 import iv.vfs.io;
27 import xiniz;
28 import xmodel;
29 import booktext;
32 // ////////////////////////////////////////////////////////////////////////// //
33 __gshared string RcDir = "~/.xreader";
34 __gshared string stateFileName;
36 shared static this () {
37 import std.path;
38 RcDir = RcDir.expandTilde.absolutePath;
39 //writeln(RcDir);
42 __gshared float shipAngle = 0;
43 __gshared bool showShip = false;
45 __gshared EliteModel shipModel;
48 // ////////////////////////////////////////////////////////////////////////// //
49 __gshared string textFontName = "~/ttf/ms/arial.ttf:noaa";
50 __gshared string textiFontName = "~/ttf/ms/ariali.ttf:noaa";
51 __gshared string textbFontName = "~/ttf/ms/arialbd.ttf:noaa";
52 __gshared string textzFontName = "~/ttf/ms/arialbi.ttf:noaa";
53 __gshared string monoFontName = "~/ttf/ms/cour.ttf:noaa";
54 __gshared string monoiFontName = "~/ttf/ms/couri.ttf:noaa";
55 __gshared string monobFontName = "~/ttf/ms/courbd.ttf:noaa";
56 __gshared string monozFontName = "~/ttf/ms/courbi.ttf:noaa";
57 __gshared string uiFontName = "~/ttf/ms/verdana.ttf:noaa";
58 __gshared string galmapFontName = "~/ttf/ms/verdana.ttf:noaa";
59 __gshared int GWidth = 900;
60 __gshared int GHeight = 1024;
61 __gshared int fsizeText = 24;
62 __gshared int fsizeUI = 16;
63 __gshared int fGalMapSize = 16;
64 __gshared bool sbLeft = false;
65 __gshared bool interAllowed = false;
67 __gshared bool flagNanoAA = false;
68 __gshared bool flagNanoSS = false;
69 __gshared bool flagNanoFAA = false;
71 __gshared NVGColor colorDim = NVGColor(0, 0, 0, 0);
72 __gshared NVGColor colorBack = NVGColor(0x2a, 0x2a, 0x2a);
73 __gshared NVGColor colorText = NVGColor(0xff, 0x7f, 0x00);
74 __gshared NVGColor colorTextHi = NVGColor(0xff, 0xff, 0x00);
75 __gshared NVGColor colorTextHref = NVGColor(0, 0, 0x80);
77 __gshared int uiFont, galmapFont;
78 __gshared int textFont, textiFont, textbFont, textzFont;
79 __gshared int monoFont, monoiFont, monobFont, monozFont;
81 __gshared bool optJustify = false;
84 enum MinWinWidth = 320;
85 enum MinWinHeight = 240;
88 // ////////////////////////////////////////////////////////////////////////// //
89 BookInfo[] loadDetailedHistory () {
90 BookInfo[] res;
91 try {
92 import std.path;
93 foreach (string line; VFile(buildPath(RcDir, ".lastfile")).byLineCopy) {
94 line = xstrip(line);
95 if (line.length == 0 || line[0] == '#' || line[0] == ';') continue;
96 try {
97 res ~= loadBookInfo(line);
98 res[$-1].diskfile = line.idup;
99 } catch (Exception) {}
101 } catch (Exception) {}
102 return res;
106 // ////////////////////////////////////////////////////////////////////////// //
107 __gshared string[] eliteShipFiles;
110 void loadEliteShips () {
111 import std.path;
112 import std.path : extension;
113 vfsAddPak(buildPath(RcDir, "eliteships.zip"), "oxm:");
114 //scope(exit) vfsRemovePak(buildPath(RcDir, "eliteships.zip"), "oxm:");
115 foreach (immutable idx, ref de; vfsFileList) {
116 //writeln("<", de.name, ">");
117 if (de.name.length > 4 && de.name[0..4] == "oxm:" && strEquCI(de.name.extension, ".oxm")) {
119 import core.memory : GC;
120 try {
121 auto mdl = new EliteModel(de.name);
122 mdl.freeData();
123 mdl.destroy;
124 //eliteShips ~= mdl;
125 eliteShipFiles ~= de.name;
126 } catch (Exception e) {}
127 GC.collect();
129 eliteShipFiles ~= de.name;
135 // ////////////////////////////////////////////////////////////////////////// //
136 VFile fwritef(A...) (VFile fl, string fmt, /*lazy*/ A args) {
137 import std.string : format;
138 auto s = format(fmt, args);
139 if (s.length) fl.rawWriteExact(s[]);
140 return fl;
144 // ////////////////////////////////////////////////////////////////////////// //
145 void readConfig () {
146 import std.path;
147 VFile fl;
148 try {
149 fl = VFile(buildPath(RcDir, ".config.ui"));
150 } catch (Exception) {
151 try {
152 try { import std.file; mkdirRecurse(RcDir); } catch (Exception) {}
153 fl = VFile(buildPath(RcDir, ".config.ui"), "w");
154 fl.fwritef("width=%s\n", GWidth);
155 fl.fwritef("height=%s\n", GHeight);
156 fl.fwritef("font-text=%s\n", textFontName);
157 fl.fwritef("font-text-italic=%s\n", textiFontName);
158 fl.fwritef("font-text-bold=%s\n", textbFontName);
159 fl.fwritef("font-text-bold-italic=%s\n", textzFontName);
160 fl.fwritef("font-mono=%s\n", monoFontName);
161 fl.fwritef("font-mono-italic=%s\n", monoiFontName);
162 fl.fwritef("font-mono-bold=%s\n", monobFontName);
163 fl.fwritef("font-mono-bold-italic=%s\n", monozFontName);
164 fl.fwritef("font-ui=%s\n", uiFontName);
165 fl.fwritef("font-galmap=%s\n", galmapFontName);
166 fl.fwritef("text-size=%s\n", fsizeText);
167 fl.fwritef("ui-text-size=%s\n", fsizeUI);
168 fl.fwritef("galmap-text-size=%s\n", fGalMapSize);
169 fl.fwritef("scrollbar-on-left=%s\n", sbLeft);
170 fl.fwritef("interference=%s\n", interAllowed);
171 fl.fwritef("color-dim=0x%08x\n", colorDim.asUintHtml);
172 fl.fwritef("color-back=0x%08x\n", colorBack.asUintHtml);
173 fl.fwritef("color-text=0x%08x\n", colorText.asUintHtml);
174 fl.fwritef("color-text-hi=0x%08x\n", colorTextHi.asUintHtml);
175 fl.fwritef("color-text-href=0x%08x\n", colorTextHref.asUintHtml);
176 fl.fwritef("nano-aa=%s\n", flagNanoAA);
177 fl.fwritef("nano-ss=%s\n", flagNanoSS);
178 fl.fwritef("nano-faa=%s\n", flagNanoFAA);
179 fl.fwritef("justify-text=%s\n", optJustify);
180 } catch (Exception) {}
181 return;
183 xiniParse(fl,
184 "font-text", &textFontName,
185 "font-text-italic", &textiFontName,
186 "font-text-bold", &textbFontName,
187 "font-text-bold-italic", &textzFontName,
188 "font-text-italic-bold", &textzFontName,
189 "font-mono", &monoFontName,
190 "font-mono-italic", &monoiFontName,
191 "font-mono-bold", &monobFontName,
192 "font-mono-bold-italic", &monozFontName,
193 "font-mono-italic-bold", &monozFontName,
194 "font-ui", &uiFontName,
195 "font-galmap", &galmapFontName,
196 "width", &GWidth,
197 "height", &GHeight,
198 "text-size", &fsizeText,
199 "ui-text-size", &fsizeUI,
200 "galmap-text-size", &fGalMapSize,
201 "scrollbar-on-left", &sbLeft,
202 "interference", &interAllowed,
203 "color-dim", &colorDim,
204 "color-back", &colorBack,
205 "color-text", &colorText,
206 "color-text-hi", &colorTextHi,
207 "color-text-href", &colorTextHref,
208 "nano-aa", &flagNanoAA,
209 "nano-ss", &flagNanoSS,
210 "nano-faa", &flagNanoFAA,
211 "justify-text", &optJustify,
216 // ////////////////////////////////////////////////////////////////////////// //
217 bool isComment (const(char)[] s) {
218 while (s.length && s.ptr[0] <= ' ') s = s[1..$];
219 return (s.length > 0 && s.ptr[0] == '#');