Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: asprin on September 15, 2019, 10:24:54 AM

Title: Creating a mod from scratch?
Post by: asprin on September 15, 2019, 10:24:54 AM
Hi everyone,

New to SMF here. But not new to the world of community forums. I've an idea about building a modification/add-on on SMF. But the thing is I couldn't find any help/documentation about building a modification from scratch. Is there an example thread which serves as a tutorial for this topic?

For the record, I'm very much experienced in

So I believe I meet the per-requisites for building a modification. The only question is how?

To give a brief overview of what I'm currently searching for:


I hope the above is making sense.
[/list]
Title: Re: Creating a mod from scratch?
Post by: Kindred on September 15, 2019, 10:33:32 AM
https://wiki.simplemachines.org/smf/Coding_Guidelines
https://wiki.simplemachines.org/smf/Category:Package_SDK
https://wiki.simplemachines.org/smf/Category:Integrating_SMF

Title: Re: Creating a mod from scratch?
Post by: asprin on September 16, 2019, 01:10:14 AM
Thanks Kindred. I had already gone through those and while they are definitely helpful, I didn't find an example so to speak. Those look more like guidelines/best practices.

Is there a tutorial (either here in the community or external) where the author shows how a mod is actually created?

Also, from the links above I see a concept called as SSI. From what I understand, this file helps in passing data to external pages (non-SMF). So does that mean SMF has no ability to create pages that are internal to the framework?
Title: Re: Creating a mod from scratch?
Post by: Arantor on September 16, 2019, 02:40:56 AM
Sure it does.

The problem with writing a mod is that for the most part you're changing the core code so there's not really a lot to show you except for how to write the same mod over and over.

In your case, start in index.php and add a new action to the list to make the new URL for your form, you add the name of the action, the file to load for that action and the function to call when you get there.

Then create a new file for your new page in Sources, put a function in it. This is where your handling will go.

To display the form, make a file in Themes/default that has a template.php suffix, inside which is going to be a function called template_something, which will contain your form. Then in your Sources function, set $context['sub_template'] to the "something" in your template_something. This renders it.

Displaying on a new page, same process.

Not sure I understand what you mean in your last point about linking a board to something.
Title: Re: Creating a mod from scratch?
Post by: asprin on September 16, 2019, 09:30:48 AM
Quote from: Arantor on September 16, 2019, 02:40:56 AM
In your case, start in index.php and add a new action to the list to make the new URL for your form, you add the name of the action, the file to load for that action and the function to call when you get there.

Then create a new file for your new page in Sources, put a function in it. This is where your handling will go.

Sounds like a start. Thanks. I'll see how far I can go with that. Btw, are you referring to the "$actionArray" variable in the index.php file?

Quote from: Arantor on September 16, 2019, 02:40:56 AM
Not sure I understand what you mean in your last point about linking a board to something.
My apologies. I think I'll be able to explain it better once I've something started and ready to show.
Title: Re: Creating a mod from scratch?
Post by: Arantor on September 16, 2019, 09:59:09 AM
Yes, I was referring to $actionArray.
Title: Re: Creating a mod from scratch?
Post by: asprin on September 20, 2019, 12:46:20 PM
Made some progress. This is exciting.

Attaching screenshot.
Title: Re: Creating a mod from scratch?
Post by: Suki on September 20, 2019, 12:52:03 PM
Are you hosting your code in any software version control website such as GitHub or bitbucket? if so we can take a look at it and offer more refined help.
Title: Re: Creating a mod from scratch?
Post by: asprin on September 20, 2019, 01:08:00 PM
Quote from: Suki on September 20, 2019, 12:52:03 PM
Are you hosting your code in any software version control website such as GitHub or bitbucket? if so we can take a look at it and offer more refined help.

No, I haven't set one up yet. But I like the idea of having guidance. Will see what I can do about that.
Title: Re: Creating a mod from scratch?
Post by: asprin on September 22, 2019, 04:40:54 AM
So done with the frontend part for now.

Coming to processing the form, what you folks suggest would be the ideal approach?

(a) Native form submission via
<form action="'.$scripturl.'?action=postevent" method="post" autocomplete="off">'

(b) Via Ajax (prefer to user jQuery here but don't mind using vanilla javascript either)

Any suggestions on form tokens for security purposes?
Title: Re: Creating a mod from scratch?
Post by: Arantor on September 22, 2019, 04:57:02 AM
SMF generally does native forms mostly because it tries to avoid relying on JS for things.

That said, it's your mod, you can build it how you like.

As far as security goes, the minimum accepted level is to pass the session values through a hidden input (using $context['session_var'] as the key and $context['session_id'] as the value), with a checkSession call when checking the submission.

You can upgrade this to a one time token (on top) in 2.1 by using createToken to make a token, putting it in the form in much the same way (I forget exactly how off hand but most forms will give you an example) and then using validateToken at submission.
Title: Re: Creating a mod from scratch?
Post by: asprin on September 22, 2019, 05:20:15 AM
Quote from: Arantor on September 22, 2019, 04:57:02 AM
As far as security goes, the minimum accepted level is to pass the session values through a hidden input (using $context['session_var'] as the key and $context['session_id'] as the value), with a checkSession call when checking the submission.

Awesome. Granted that I still have to go through most of the manuals related to SMF, but loving these bits of information. I think I'll leave out tokens for now and follow the above approach.
Title: Re: Creating a mod from scratch?
Post by: Arantor on September 22, 2019, 05:21:32 AM
Honestly, the greatest manual to SMF development is SMF's own code. Whenever I wrote mods, I studied the code to see what it did and to try to replicate the behaviours I wanted.
Title: Re: Creating a mod from scratch?
Post by: asprin on September 24, 2019, 11:59:58 AM
More updates. Have decided to transform my idea into a vBookie/Sportsbook mod. This would involve


Loving the way it's been coming out.
Title: Re: Creating a mod from scratch?
Post by: Aleksi "Lex" Kilpinen on September 24, 2019, 01:47:26 PM
Quote from: asprin on September 16, 2019, 01:10:14 AM
Thanks Kindred. I had already gone through those and while they are definitely helpful, I didn't find an example so to speak. Those look more like guidelines/best practices.

Is there a tutorial (either here in the community or external) where the author shows how a mod is actually created?
A little late to the party, but the modsite ( https://custom.simplemachines.org/mods/ ) actually shows you all edits done for any mod you can find, and you can download the packages, extract them, and study what is not done through direct edits. That's pretty much how I have learned what I have about how SMF code functions.
Title: Re: Creating a mod from scratch?
Post by: asprin on October 07, 2019, 01:13:15 PM
Quote from: Aleksi "Lex" Kilpinen on September 24, 2019, 01:47:26 PM
A little late to the party, but the modsite ( https://custom.simplemachines.org/mods/ ) actually shows you all edits done for any mod you can find, and you can download the packages, extract them, and study what is not done through direct edits. That's pretty much how I have learned what I have about how SMF code functions.

Thanks for the tip!

A little more progress made today, although work is keeping me busy of late.

Finished with editing and deleting an outcome. Next move is a major one where a user will be able to place a wager/bet on an outcome.

Title: Re: Creating a mod from scratch?
Post by: asprin on October 08, 2019, 10:00:53 AM
So, I'm about to start work on letting a user place a wager on an outcome. But before I do that, I need to know how much money/cash/vcash does the member have in his/her account.

From the mods section, I found two mods that deal with this virtual currency thing:

By going through the code of these mods, I found that SMF Shop uses a column named "money" and ST Shop uses a column named "shopMoney" to store the user's balance.

So I was wondering if there was a way to determine if any of these columns is present in the "members" table and if not, I go ahead and create a new one to store the balance. Is this something that can be achieved via $smcFunc ? (the checking of those two columns)

So basically something like the below:
if(<members table has either "money" or "shopMoney" column>) // need help with this condition
{
      // fetch the current balance from that column (I can handle this part)
}
else
{
     // create a new column in the members table (I can handle this part)
}
Title: Re: Creating a mod from scratch?
Post by: Arantor on October 08, 2019, 10:05:44 AM
Honestly, I wouldn't try to guess based on presence of column (because it's possible a user had at one time installed one, uninstalled it leaving the data in the database, and installed the other), but simply ask the user.

What you could do is call db_extend('Packages'); to load the extra functions available there, then use $smcFunc['db_list_columns']() to get the column names. You wouldn't want to do this on the fly because it's not the cheapest query ever but you certainly could do this in the installer and admin areas.

I'd probably add a new column anyway, just for your use, then let the user choose whether to use yours or another if it happens to be around.
Title: Re: Creating a mod from scratch?
Post by: asprin on October 08, 2019, 10:16:38 AM
Quote from: Arantor on October 08, 2019, 10:05:44 AM
Honestly, I wouldn't try to guess based on presence of column (because it's possible a user had at one time installed one, uninstalled it leaving the data in the database, and installed the other), but simply ask the user.
That's an excellent point. I had installed ST Shop a few weeks back and then uninstalled it. And now when I go and look in the members table, the ST Shop columns are still present. I simply assumed that the modder would take care of removing the columns as well upon uninstallation, but I guess I was wrong.

Quote from: Arantor on October 08, 2019, 10:05:44 AM
What you could do is call db_extend('Packages'); to load the extra functions available there, then use $smcFunc['db_list_columns']() to get the column names. You wouldn't want to do this on the fly because it's not the cheapest query ever but you certainly could do this in the installer and admin areas.
What does this do exactly? From the naming convention, it does look like it will list out the columns of a table, but wouldn't this again return the columns of an un-installed mod too? i.e. the same problem as the above point?


Quote from: Arantor on October 08, 2019, 10:05:44 AM
I'd probably add a new column anyway, just for your use, then let the user choose whether to use yours or another if it happens to be around.
This sounds like a safe bet. Let the user decide from the Admin section which column he intends to use.

Thanks for the valuable inputs.
Title: Re: Creating a mod from scratch?
Post by: Illori on October 08, 2019, 10:18:00 AM
depending on how the mod package is created, it may offer to the users to remove the left over data in the database. otherwise it most likely would stay in the database forever.
Title: Re: Creating a mod from scratch?
Post by: Arantor on October 08, 2019, 10:22:24 AM
QuoteI simply assumed that the modder would take care of removing the columns as well upon uninstallation, but I guess I was wrong.

Not necessarily. It's an old mod and might not support the code for dealing with this.

If you create the column with $smcFunc['db_create_table'] or $smcFunc['db_add_column'] during installation, and specifically you've put the code in question into a file referenced with a <database> tag in the package-info.xml file, SMF will track what was added during installation and offer you the choice to remove it on uninstall. But if any of these steps were skipped, it won't track it and be left behind (on the theory that it might be upgraded so it should keep the data)

QuoteWhat does this do exactly? From the naming convention, it does look like it will list out the columns of a table, but wouldn't this again return the columns of an un-installed mod too? i.e. the same problem as the above point?

It does exactly that, it lists the columns. The problem is, you can detect which options *might* be available (e.g. you might detect that ST Shop was installed previously) and let the user choose that, but you can't reliably infer that ST Shop is *currently* installed - because that path lies madness if you go into the drama of working out what other mods might be supported based on file detection or function existence, unless there's a stable API you can query in SMF that you can rely on, but there isn't. (Other systems do this in ways you can rely on. SMF, currently, doesn't, mostly because it also uses the package manager to ship its upgrades)

QuoteLet the user decide from the Admin section which column he intends to use.

I wouldn't phrase it as 'which column' in the prompt; I'd phrase it more, "We've detected you might have had ST Shop installed previously, would you like to use the currency from that" or something like that, just because most users are not technical enough to look at the database, but they might remember which shop mod they were using.
Title: Re: Creating a mod from scratch?
Post by: Diego Andrés on October 08, 2019, 02:35:59 PM
For instance I don't think SMF Shop supports SMF 2.1 atm so if you are developing for 2.1, that rules out one option, at least for now.
Title: Re: Creating a mod from scratch?
Post by: vbgamer45 on October 08, 2019, 03:41:03 PM
Quote from: Diego Andrés on October 08, 2019, 02:35:59 PM
For instance I don't think SMF Shop supports SMF 2.1 atm so if you are developing for 2.1, that rules out one option, at least for now.
Yes it does not. I don't plan to update it at this time.
Title: Re: Creating a mod from scratch?
Post by: asprin on October 09, 2019, 04:31:05 AM
Quote from: Diego Andrés on October 08, 2019, 02:35:59 PM
For instance I don't think SMF Shop supports SMF 2.1 atm so if you are developing for 2.1, that rules out one option, at least for now.

Yes, for now it's only for 2.1 onwards. I've so much to learn about this software so I've decided to not worry about backward compatability and focus of the current and future versions only.
Title: Re: Creating a mod from scratch?
Post by: OtterlyAdorable on February 14, 2020, 02:05:43 PM
Is this still in the works? I'm looking for a more comprehensive dice tool for our forum!
Title: Re: Creating a mod from scratch?
Post by: vbgamer45 on February 14, 2020, 02:25:22 PM
I believe the author has left SMF but might be open to paid work.