Skip to content

Commit

Permalink
Merge pull request #5 from VertexDezign/fs19-fix-modsettings-not-created
Browse files Browse the repository at this point in the history
🚑 Create modSettings folder
  • Loading branch information
Grisu118 authored Apr 17, 2019
2 parents 182c149 + a9a5f52 commit ea4e634
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# RealClock

[![ModHub Download](https://img.shields.io/badge/%5BFS17%5D%20ModHub-2.0.1.0-green.svg?style=flat-square)](https://farming-simulator.com/mod.php?lang=de&country=ch&mod_id=51459&title=fs2017)
[![ModHub Download](https://img.shields.io/badge/%5BFS19%5D%20ModHub-1.1.0.0-blue.svg?style=flat-square)](https://farming-simulator.com/mod.php?lang=de&country=ch&mod_id=117935&title=fs2019)
[![ModHub Download](https://img.shields.io/badge/%5BFS19%5D%20ModHub-1.1.1.0-blue.svg?style=flat-square)](https://farming-simulator.com/mod.php?lang=de&country=ch&mod_id=117935&title=fs2019)

A small script, which shows the actual time in the upper right corner.

Expand Down
99 changes: 61 additions & 38 deletions RealClock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
-- v2.0 - 2017-09-23 - Add config xml
-- v2.0.1 - 2018-01-13 - Do not render clock when renderTime is false
-- v2.1 - 2019-01-14 - Move settings file to modSettings folder, do not draw when showTime is false
-- v2.1.1 - 2019-04-01 - Create modSettings folder
-- @descripion: Shows the real time clock in the upper right corner
-- @web: http://grisu118.ch or http://vertexdezign.net
-- Copyright (C) Grisu118, All Rights Reserved.

RealClock = {}
RealClock.configFileName = "realClock.xml"
RealClock.d = {};
RealClock.d = {}
RealClock.d.timeFormat = "%H:%M:%S"
RealClock.d.position = {}
RealClock.d.position.dynamic = true
Expand All @@ -24,13 +25,15 @@ RealClock.d.rendering.color = "white"
RealClock.d.rendering.fontSize = 0.015

local function protect(tbl)
return setmetatable({}, {
__index = tbl,
__newindex = function(t, key, value)
error("attempting to change constant " ..
tostring(key) .. " to " .. tostring(value), 2)
end
})
return setmetatable(
{},
{
__index = tbl,
__newindex = function(t, key, value)
error("attempting to change constant " .. tostring(key) .. " to " .. tostring(value), 2)
end
}
)
end

-- Protect the defaults, nobody should overwrite them!
Expand All @@ -52,16 +55,21 @@ function RealClock:loadMap(name)
self.timeFormat = RealClock.d.timeFormat

if g_dedicatedServerInfo == nil then
local xmlFile = g_modsDirectory .. "/" .. RealClock.configFileName;
local xmlFile = g_modsDirectory .. "/" .. RealClock.configFileName
if not fileExists(xmlFile) then
local newXmlFile = getUserProfileAppPath() .. "modsSettings/" .. RealClock.configFileName;
local modSettingsDir = getUserProfileAppPath() .. "modsSettings"
local newXmlFile = modSettingsDir .. RealClock.configFileName
if not fileExists(newXmlFile) then
self:writeDefaultConfig(newXmlFile);
createFolder(modSettingsDir)
self:writeDefaultConfig(newXmlFile)
end
self:setValuesFromXML(newXmlFile)
else
self.debugger:warn("Loading configuration from mod folder, this is deprecated and will be removed in a furhter version. Move your " .. RealClock.configFileName .. " in the modSettings folder.")
self:setValuesFromXML(xmlFile);
self.debugger:warn(
"Loading configuration from mod folder, this is deprecated and will be removed in a furhter version. Move your " ..
RealClock.configFileName .. " in the modSettings folder."
)
self:setValuesFromXML(xmlFile)
end
end
end
Expand Down Expand Up @@ -116,10 +124,12 @@ function RealClock:getColor(source)
end

function RealClock:writeDefaultConfig(fileName)
self.debugger:info(function()
return "Write default Config to " .. fileName
end)
local xml = createXMLFile("RealClock", fileName, "RealClock");
self.debugger:info(
function()
return "Write default Config to " .. fileName
end
)
local xml = createXMLFile("RealClock", fileName, "RealClock")
setXMLBool(xml, "RealClock.position#isDynamic", RealClock.d.position.dynamic)
setXMLFloat(xml, "RealClock.position#x", RealClock.d.position.x)
setXMLFloat(xml, "RealClock.position#y", RealClock.d.position.y)
Expand All @@ -131,25 +141,33 @@ function RealClock:writeDefaultConfig(fileName)
end

function RealClock:setValuesFromXML(fileName)
self.debugger:info(function()
return "Read from xml " .. fileName
end)
self.debugger:info(
function()
return "Read from xml " .. fileName
end
)
local xml = loadXMLFile("RealClock", fileName)
self.position.dynamic = Utils.getNoNil(getXMLBool(xml, "RealClock.position#isDynamic"), RealClock.d.position.dynamic);
self.debugger:debug(function()
return "Position.dynamic: " .. tostring(self.position.dynamic)
end)
self.position.dynamic = Utils.getNoNil(getXMLBool(xml, "RealClock.position#isDynamic"), RealClock.d.position.dynamic)
self.debugger:debug(
function()
return "Position.dynamic: " .. tostring(self.position.dynamic)
end
)
local x = Utils.getNoNil(getXMLFloat(xml, "RealClock.position#x"), RealClock.d.position.x)
self.debugger:debug(function()
return "Position.x: " .. tostring(x)
end)
self.debugger:debug(
function()
return "Position.x: " .. tostring(x)
end
)
if (self:validateFloat(x, "x")) then
self.position.x = x
end
local y = Utils.getNoNil(getXMLFloat(xml, "RealClock.position#y"), RealClock.d.position.y)
self.debugger:debug(function()
return "Position.y: " .. tostring(y)
end)
self.debugger:debug(
function()
return "Position.y: " .. tostring(y)
end
)
if (self:validateFloat(y, "y")) then
self.position.y = y
end
Expand All @@ -171,9 +189,12 @@ function RealClock:setValuesFromXML(fileName)
self.rendering.color = RealClock.d.rendering.color
end
else
self.debugger:warn(function()
return "Invalid value for color, only 'white', 'black' or a custom color like '0,0.5,1,0.8' allowed, found " .. color
end)
self.debugger:warn(
function()
return "Invalid value for color, only 'white', 'black' or a custom color like '0,0.5,1,0.8' allowed, found " ..
color
end
)
self.rendering.color = RealClock.d.rendering.color
end
else
Expand All @@ -182,7 +203,7 @@ function RealClock:setValuesFromXML(fileName)

local fontSize = Utils.getNoNil(getXMLFloat(xml, "RealClock.rendering#fontSize"), RealClock.d.rendering.fontSize)
if self:validateFloat(fontSize, "fontSize") then
self.rendering.fontSize = fontSize;
self.rendering.fontSize = fontSize
end
self.timeFormat = Utils.getNoNil(getXMLString(xml, "RealClock.format#string"), RealClock.d.timeFormat)
delete(xml)
Expand All @@ -193,11 +214,13 @@ function RealClock:validateFloat(float, text)
if f ~= nil and f >= 0 and f <= 1 then
return true
else
self.debugger:warn(function()
return "Invalid value for " .. text .. ", must be a value between 0 and 1 including both, was " .. float
end)
self.debugger:warn(
function()
return "Invalid value for " .. text .. ", must be a value between 0 and 1 including both, was " .. float
end
)
return false
end
end

addModEventListener(RealClock)
addModEventListener(RealClock)
10 changes: 7 additions & 3 deletions modDesc.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<modDesc descVersion="41">
<author>Vertexdezign, Slivicon</author>
<version>1.1.0.0</version>
<modDesc descVersion="43">
<author>VertexDezign and Slivicon</author>
<version>1.1.1.0</version>
<title>
<en>RealClock Mod</en>
<de>Echte Uhrzeit Mod</de>
Expand All @@ -23,6 +23,8 @@ This script creates a configuration xml at first startup in your modfolder. Ther
* fontSize: the size of the font to display, a value in the range [0..1], default 0.015. UI Scale is used for calculating the font size to display
* Format of datetime string (placeholders see formats_en.txt)
Changelog 1.1.1.0:
- Create modSettings folder if not existent
Changelog 1.1.0.0:
- Improve description
- Move settings xml to modSettings folder
Expand All @@ -45,6 +47,8 @@ Das Script erstellt beim ersten Start eine xml Datei im Mod Ordner. Dort könnt
* fontSize: Die grösse der Schrift, ein wert im Bereich [0..1], Standart ist 0.015. UI Skalierung wird mit einbezogen bei der Darstellung!
* Format des Strings (Formatierungsmöglichkeiten siehe formats_de.txt)
Changelog 1.1.1.0:
- modSettings Ordner wird erstellt, falls er noch nicht existiert
Changelog 1.1.0.0:
- Verbesserte Beschreibung
- Verschieben der settings xml in den modSettings Ordner
Expand Down

0 comments on commit ea4e634

Please sign in to comment.