Read custom game config file

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

DarkSlash
Posts: 30
Joined: 12 Feb 2016, 18:15

Read custom game config file

Postby DarkSlash » 22 Feb 2016, 18:47

I need a config file for my game with some values, so if I need to balance my game, users don't need to update the whole client, the server takes care of it.
So I made a txt file and read data from it (in the single player version). In multiplayer, instead of fetching the file from some random url, I want that the server fetch the file from a directory and passes that values. Is this still correct in this architecture?

If everything is ok, should I place the file in the SFS2X root directory or is recommended some location for improved security?
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Read custom game config file

Postby Lapo » 23 Feb 2016, 08:47

Hi,
we already provide a simple mechanism for loading custom configs in your Extension, via properties files.

By default the server expects a file called config.properties in the same folder where you deploy your Extension's jar files.
The .properties file is a simple key-value test file such as:

Code: Select all

fruit=apple
color=yellow
taste=sweet


In your init method you can access these values like this:

Code: Select all

@Override
public void init()
{
   String fruit = getConfigProperties().get("fruit");
   String color = getConfigProperties().get("color");
   // etc...
}


All values are returned as String, so if you're using numbers or booleans you will need to cast them appropriately.

Check the documentation here for more details:
http://docs2x.smartfoxserver.com/Advanc ... extensions
under the Custom configuration section

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
DarkSlash
Posts: 30
Joined: 12 Feb 2016, 18:15

Re: Read custom game config file

Postby DarkSlash » 23 Feb 2016, 14:15

Great! And if I change that file, the extension automatically reloads it or I have to restart the server?
DarkSlash
Posts: 30
Joined: 12 Feb 2016, 18:15

Re: Read custom game config file

Postby DarkSlash » 23 Feb 2016, 14:42

And there's a special syntax for this file? JSON? XML? Custom? I need to retrieve an int[][] can I from that .properties file?
I looked into the Unity examples and can't find a .properties file to look how it's done

EDIT: getConfigProperties is not found and no dependency is auto imported on Intellij Idea
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Read custom game config file

Postby Lapo » 23 Feb 2016, 16:28

DarkSlash wrote:Great! And if I change that file, the extension automatically reloads it or I have to restart the server?

No, the file is not automatically reloaded, but you can force an Extension reload which will also trigger the reloading of the file itself.

This is done via the Admin Tool > Zone Manager: choose your Zone, select the Extension tab and hit Reload.

And there's a special syntax for this file? JSON? XML? Custom? I need to retrieve an int[][] can I from that .properties file?

Properties files in java are key,value string pairs. There is no JSON or XML parsing involved.
Every pair of strings is written on one line, as the example I provided earlier.

If you need to define multidimensional arrays of data then you probably need something more advanced, such as JSON. You can store your own text file in the Extension folder and then load it in in the init() method of your code.

Here's an example of how to read a file into a String:
http://jdevelopment.nl/java-7-oneliner- ... le-string/

Then you can decode it with the JSON library included in SFS2X:

Code: Select all

JSONObject jso = JSONObject.fromObject(jsonString);


This in turn requires that you add lib/json-lib-2.2.3-jdk15.jar to your project's dependencies.

hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Read custom game config file

Postby mmilen » 08 Dec 2020, 21:06

What would be the path to the Extention folder location? I need to read a file from the extension folder.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Read custom game config file

Postby Lapo » 09 Dec 2020, 08:09

Hi,
from within your Extension call this method:

Code: Select all

String path = getCurrentFolder();


Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Read custom game config file

Postby mmilen » 09 Dec 2020, 16:14

That was simple.
Thanks for the prompt response.

On a different matter, do you know anyone that had successfully implemented Firestore database with an extension. I need to dump some text data, but running in trouble with the Google Credentials and jar dependencies. Willing to pay for the working sample code and proper set of jars I need to add to SFS. When I pull the Google libraries from Maven SFS complains about the version of some of the dependency. It was a pain to go search for each specific version to silence SFS error. I'm running test environment SFS 2.16.3 on MacOS Big Sur.

If I open my Firestore collection for the public to create documents, I have no trouble using Google's RESR API.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Read custom game config file

Postby Lapo » 10 Dec 2020, 10:13

Hi,
I am not aware of customers using Firestore with SFS2X, although many don't tell us what they use :)
When I pull the Google libraries from Maven SFS complains about the version of some of the dependency.


I took a quick look at this:
https://github.com/googleapis/java-firestore

and it says the basic requirement is Java7, so SFS2X should work just fine as it runs with Java8 out of the box.
Shouldn't Maven take care of pulling all the necessary dependencies for the project?

Unfortunately I know very little about Firestore, so I'd recommend checking their docs for further help.
Lapo

--

gotoAndPlay()

...addicted to flash games
mmilen
Posts: 298
Joined: 09 Nov 2010, 00:48
Contact:

Re: Read custom game config file

Postby mmilen » 14 Dec 2020, 13:49

Thanks and Happy Holidays !!!
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Read custom game config file

Postby Lapo » 14 Dec 2020, 16:29

You too :)
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 36 guests