News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

How to replace code with empty space using package-info.xml

Started by MESWEB, October 03, 2015, 07:52:14 PM

Previous topic - Next topic

MESWEB

I wonder if this is possible:
1. Make mod
2. Install mod
    a)Search code to add before
<file name="$sourcedir/Subs.php">

<operation>
<search position="after"><![CDATA[// Generate a random seed and ensure it's stored in settings.
function smf_seed_generator()]]></search>
<add><![CDATA[// Bla Bla
]]></add>
</operation>
</file>

3. Uninstall mod
    a)Search code to replace with: " " (empty space or new line or tab)
<file name="$sourcedir/Subs.php">

<operation>
<search position="replace"><![CDATA[]]></search>
<add><![CDATA[// Bla Bla
]]></add>
</operation>
</file>


Kindred

Simple, Don't use replace on the removal if you use add before on the addition
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

MESWEB

My bad - should be "after"
So do You have any idea how to replace code with empty space?

Gluz

Take a look at the package-info.xml SDK and modification.xml SDK.

For uninstall, you just need to specify in the package-info.xml this: <modification reverse="true">modification.xml</modification> and it will undo the changes, no need to replace by empty spaces to uninstall. Your modification.xml (where the file operations are declared) just remain intact, the same is used to uninstall.

MESWEB

I know about this reverse option in modification tag. But issue is in modified code by other mod which one add code before You search code then when You need uninstall Your mod can't be done without errors because previous mod rewrite code. For example:
Mod 1:
This mod search in code:
//there is mod settings
This mod add some code before:
//this is mod 1
some code of mod 1

Mod 2:
This mod search in code:
//there is mod settings
This mod add some code before:
//this is mod 2
some code of mod 2

If You try uninstall mod 1 then You should see error because mod 2 add code between:
"//there is mod settings" and "this is mod 1"

margarett

You shouldn't replace with empty because it's impossible to revert it. Use replace and comment the code block instead ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

MESWEB

If I comment every time the code should get lot of this same comments if of course I install mod again and again. I prefer to replace with empty space but smf parser can't do that, I don't know why but this issue should be fixed necessary.

Kindred

it's not an issue nor does it need fixing.   You are trying to do something that the package manager was not intended to do.

When mods uninstall -- they should be doing the reverse of what they did to install.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

MESWEB

I know what are You try tell to me. Look at this from different side. If You got lot of mods which one editing this same file on this same line then You have got problem. When You uninstall every mod without specific order (order by installation) then You should see an error. For example this is a list of installed mods and this mods are edit this same file on this same line:
1. Mod
2. Mod
3. Mod
4. Mod
5. Mod
6. Mod
So try to uninstall mod 4 without error. This is in smf impossible! Coz when You try uninstall this mod then mod 4 expect clean code without any extra code. Why? Because Mod 5,6 add own code after Mod 4. If I'm wrong show me how should look uninstall file on this example below:
<?php
if (isset(
$context['menu_buttons'][$context['current_action']]))
$current_action $context['current_action'];
elseif ($context['current_action'] == 'search2')
$current_action 'search';
elseif ($context['current_action'] == 'theme')
$current_action = isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'pick' 'profile' 'admin';
elseif ($context['current_action'] == 'register2')
$current_action 'register';
elseif ($context['current_action'] == 'login2' || ($user_info['is_guest'] && $context['current_action'] == 'reminder'))
$current_action 'login';
elseif ($context['current_action'] == 'groups' && $context['allow_moderation_center'])
$current_action 'moderate';

$context['menu_buttons'][$current_action]['active_button'] = true;

if (!$user_info['is_guest'] && $context['user']['unread_messages'] > && isset($context['menu_buttons']['pm']))
{
$context['menu_buttons']['pm']['alttitle'] = $context['menu_buttons']['pm']['title'] . ' [' $context['user']['unread_messages'] . ']';
$context['menu_buttons']['pm']['title'] .= ' [<strong>' $context['user']['unread_messages'] . '</strong>]';
}
}

//This is mod nr. 1
function BlaBla()
{
global $settings;
}

//This is mod nr. 2
function BlaBla()
{
global $settings;
}

//This is mod nr. 3
function BlaBla()
{
global $settings;
}

//This is mod nr. 4
function BlaBla()
{
global $settings;
}

//This is mod nr. 5
function BlaBla()
{
global $settings;
}

//This is mod nr. 6
function BlaBla()
{
global $settings;
}

// Generate a random seed and ensure it's stored in settings.
function smf_seed_generator()
{
global $modSettings;

// Never existed?
if (empty($modSettings['rand_seed']))
{
$modSettings['rand_seed'] = microtime() * 1000000;
updateSettings(array('rand_seed' => $modSettings['rand_seed']));
}

if (@version_compare(PHP_VERSION'4.2.0') == -1)
{
$seed = ($modSettings['rand_seed'] + ((double) microtime() * 1000003)) & 0x7fffffff;
mt_srand($seed);
}

// Change the seed.
updateSettings(array('rand_seed' => mt_rand()));
}


Now try to uninstall mod 2,3,4 or 5 without error. This is no bug?

Kindred

nope... there is no bug.  it is working exactly as designed.

Keep track of the order which you installed mods...   there is even a mod to do that
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

margarett

MESWEB, I understand what you are saying and of course PackMan can't cover all cases. Yet it covers most of what I can remember ;)

- The thing with "replace" with nothing just can't happen. If you really want to do that, you need to pick a larger piece of code (which includes the code you want to remove). It's not ideal but it's the only way to revert the changes, should the user choose to uninstall your mod. If you have a better idea, please bring it ;) (just remember that several persons already thought about this issue and the general consensus is that the current implementation brings the best of several worlds with minimum inconvenience)

- several MODs all editing the same file and doing the same thing is a straight single way lane to disaster :P Imagine several MODs all trying to, dunno... Pick an image and open it in a popup! Which should do the work? Disaster :P
- common places which are expected to receive MOD changes usually have text anchors (eg, the Modification Settings page. Not all do, of course, but I'd say that most do.
- last but not least. Hooks. Hooks, hooks, hooks. The example you give is perfectly fixed by using hooks ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Joshua Dickerson

Try to use hooks. There is a good chance you will find a hook that you can use to trigger whatever code you're trying to run.
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Advertisement: