|
| 1 | +# Introduction |
| 2 | + |
| 3 | +SmallBase is a lightweight Lua framework designed to provide developers with the tools and foundations to create their own scripts, without imposing specific features. |
| 4 | + |
| 5 | +With that out of the way, thank you for considering contributing to this small project. It's people like you that make these open source projects great! |
| 6 | + |
| 7 | +## Commit Convention |
| 8 | + |
| 9 | +These are recommended, but not enforced yet. SmallBase follows similar commit guidelines to [YimMenu](https://github.com/Mr-X-GTA/YimMenu/blob/master/CONTRIBUTING.md): |
| 10 | + |
| 11 | +### Commit Structure |
| 12 | + |
| 13 | + ```text |
| 14 | + <type>(scope): <description> |
| 15 | +
|
| 16 | + [optional body] |
| 17 | +
|
| 18 | + [optional footer] |
| 19 | + ``` |
| 20 | + |
| 21 | +- **Types (lowercase only):** |
| 22 | + - feat: New features. |
| 23 | + - fix: Bug fixes. |
| 24 | + - style: Feature and updates related to styling. |
| 25 | + - refactor: Refactoring a specific section of the codebase. |
| 26 | + - test: Everything related to testing. |
| 27 | + - docs: Everything related to documentation. |
| 28 | + - chore: Regular code maintenance. |
| 29 | + |
| 30 | +- **Scope:** |
| 31 | + - A scope is a phrase describing parts of the code affected by the changes. For example `(translations)`. |
| 32 | + |
| 33 | +- **Body (Optional):** |
| 34 | + - The commit body can provide additional contextual information. For breaking changes, the body MUST start with "BREAKING CHANGE". |
| 35 | + |
| 36 | +- **Footer (Optional):** |
| 37 | + - A commit footer is used to reference issues affected by the code changes. For example: "Fixes #13". It can also be used to indicate breaking changes by starting with "BREAKING CHANGE". |
| 38 | + |
| 39 | +- **Example:** |
| 40 | + |
| 41 | + ```text |
| 42 | + fix(SomeFeature): fix constructor returning an empty object. |
| 43 | + docs(Readme): document coding conventions |
| 44 | + ``` |
| 45 | + |
| 46 | +## Coding Standards |
| 47 | + |
| 48 | +### Annotations |
| 49 | + |
| 50 | +Annotate all enums, classes, and class methods using [LuaLS](https://luals.github.io/wiki/annotations/)'s style. |
| 51 | + |
| 52 | +Annotations are critical for readability, code completion, error checking, and automatic generation of class documentations. |
| 53 | + |
| 54 | +For comments/summaries/descriptions, you can use as many dashes as you want but please leave a space between the last dash and the text. |
| 55 | + |
| 56 | +- **Example:** |
| 57 | + |
| 58 | + ```lua |
| 59 | + -- Calculates the sum of two numbers. |
| 60 | + ---@param a number The first number |
| 61 | + ---@param b number The second number |
| 62 | + ---@return number The sum of both numbers |
| 63 | + function MyClass:Add(a, b) |
| 64 | + return a + b |
| 65 | + end |
| 66 | + ``` |
| 67 | + |
| 68 | +### Global Variables |
| 69 | + |
| 70 | +There are two ways to declare globals: |
| 71 | + |
| 72 | +1. Globals that should be serialized to JSON: |
| 73 | +Index SmallBase's `GVars` table. Even if the variable was never declared before: |
| 74 | + |
| 75 | + ```lua |
| 76 | + GVars.some_feature_enabled, _ = ImGui.Checkbox("My Checkbox", GVars.some_feature_enabled) |
| 77 | + ``` |
| 78 | + |
| 79 | +2. Regular globals: |
| 80 | +Use Lua's default global table `_G`: |
| 81 | + |
| 82 | + ```lua |
| 83 | + some_global_number = 123 |
| 84 | + ``` |
| 85 | + |
| 86 | +### Style |
| 87 | + |
| 88 | +You are free to use any style you want, except in these cases: |
| 89 | + |
| 90 | +| Scope | Naming | Example | |
| 91 | +| ----------- | ----------- | ---------- | |
| 92 | +| Global Functions | PascalCase | `function DoSomething(...) end` | |
| 93 | +| Local Functions | any (consistent) | You are free to use any style as long as it stays consistent throughout the whole file | |
| 94 | +| Standard Lib Extensions | Use the lib's default style | `string.somefunc = function(...) end` | |
| 95 | +| Enums | PascalCase prefixed with a lowercase `e` | `eExampleEnum` | |
| 96 | +| Enum Members | Preferably UPPER_SNAKE_CASE but PascalCase is also allowed | `eExampleEnum.SOME_MEMBER`/`eExampleEnum.SomeMember` | |
| 97 | +| Classes | PascalCase | `MyNewClass = Class("MyNewClass")` | |
| 98 | +| Class Methods | PascalCase | `function MyNewClass:ExampleMethod(...) end` | |
| 99 | +| Class Private Variables | snake_case prefixed with an `m` | `m_handle` | |
| 100 | + |
| 101 | +### Formatting |
| 102 | + |
| 103 | +- **Indentations:** |
| 104 | + - Does not matter. If using tabs, make sure one tab equals **four** spaces. |
| 105 | + |
| 106 | +- **Line Wrapping:** |
| 107 | + - Try to wrap wide lines using either your IDE's formatter or a Pythonic way. |
| 108 | + |
| 109 | + - Example: |
| 110 | + |
| 111 | + ```lua |
| 112 | + SomeFunc(param1, param2, param3, param4, param5, param6, param7, param8, param9, ...) |
| 113 | + ``` |
| 114 | + |
| 115 | + - Preferred (Pythonic) style: |
| 116 | + |
| 117 | + ```lua |
| 118 | + SomeFunc( |
| 119 | + param1, |
| 120 | + param2, |
| 121 | + param3, |
| 122 | + param4, |
| 123 | + param5, |
| 124 | + param6, |
| 125 | + param7, |
| 126 | + param8, |
| 127 | + param9, |
| 128 | + ... |
| 129 | + ) |
| 130 | + ``` |
| 131 | + |
| 132 | +- **Nested `if` Statements:** |
| 133 | + |
| 134 | + - Always try to use guarded if statements when applicable. |
| 135 | + |
| 136 | + - Example: |
| 137 | + |
| 138 | + ```lua |
| 139 | + local cond_1 = false |
| 140 | + local cond_2 = nil |
| 141 | + local cond_3 = true |
| 142 | + |
| 143 | + if cond_1 then |
| 144 | + if cond_2 then |
| 145 | + if cond_3 then |
| 146 | + DoSomething() |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | + ``` |
| 151 | + |
| 152 | + - Preferred approach: |
| 153 | + |
| 154 | + ```lua |
| 155 | + local cond_1 = false |
| 156 | + local cond_2 = nil |
| 157 | + local cond_3 = true |
| 158 | + |
| 159 | + if not cond_1 then |
| 160 | + return |
| 161 | + end |
| 162 | + |
| 163 | + if not cond_2 then |
| 164 | + return |
| 165 | + end |
| 166 | + |
| 167 | + if not cond_3 then |
| 168 | + return |
| 169 | + end |
| 170 | + |
| 171 | + DoSomething() |
| 172 | + ``` |
| 173 | + |
| 174 | + - Shorter version: |
| 175 | + |
| 176 | + ```lua |
| 177 | + local cond_1 = false |
| 178 | + local cond_2 = nil |
| 179 | + local cond_3 = true |
| 180 | + |
| 181 | + if not (cond_1 and cond_2 and cond_3) then |
| 182 | + return |
| 183 | + end |
| 184 | + |
| 185 | + DoSomething() |
| 186 | + ``` |
| 187 | + |
| 188 | +## What To Avoid |
| 189 | + |
| 190 | +Avoid adding end-user features directly to the project (example: A drift minigame, a business manager, animations, etc.). |
| 191 | + |
| 192 | +SmallBase is, as the name suggests, a base. It provides the tools and the foundations to create new scripts. |
| 193 | + |
| 194 | +Two scripts can both use this project as a template and offer completely different sets of features. |
| 195 | + |
| 196 | +If you want to share a feature, make it a standalone module that uses SmallBase. |
0 commit comments